NC214742. Howmanysequences?
描述
输入描述
The first line contains one integer T(T ≤ 1,000), the number of test cases.For each test case, there are two integers in one line in the following format:N M(1≤ N, M ≤ 106 )
输出描述
For each group of test data, the output line contains an integer representing the number of sequences that meet the conditions after taking the modulus of 1e9 + 7.
示例1
输入:
4 1 1 3 3 2 3 3 1
输出:
1 10 4 3
C++ 解法, 执行用时: 248ms, 内存消耗: 31600K, 提交时间: 2021-07-04 16:47:53
#include<iostream> #define int long long using namespace std; const int N=2000010,mod=1e9+7; int frac[N],inv[N]; int n,m; int qsm(int x,int y){ int res=1; while(y){ if(y&1){ res=res*x%mod; } x=x*x%mod; y>>=1; } return res; } signed main(){ int t;cin>>t; frac[0]=1,inv[0]=1; for(int i=1;i<N;i++){ frac[i]=frac[i-1]*i%mod; inv[i]=qsm(frac[i],mod-2); } while(t--){ int n,m;cin>>n>>m; cout<<frac[m+n-1]*inv[m]%mod*inv[n-1]%mod<<endl; } return 0; }