AcWing 886. 求组合数 II
原题链接
简单
作者:
渐修
,
2024-04-12 13:21:22
,
所有人可见
,
阅读 3
/*
递推预处理超时,预处理阶乘mod p
c[a][b]=fact[a]/infact[b]*infact[a-b]
*/
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1e5+10,mod=1e9+7;
int n;
int fact[N],infact[N];
int qmi(int a,int b,int p)
{
int res=1%p;
while(b)
{
if(b&1)
res=(ll)res*a%p;
a=(ll)a*a%p;
b>>=1;
}
return res;
}
void init()
{
fact[0]=1,infact[0]=1;
for(int i=1;i<N;i++)
{
fact[i]=(ll)fact[i-1]*i%mod;
infact[i]=(ll)infact[i-1]*qmi(i,mod-2,mod)%mod;
}
}
int main()
{
cin>>n;
init();
while(n--)
{
int a,b;
cin>>a>>b;
cout<<(ll)fact[a]*infact[b]%mod*infact[a-b]%mod<<endl;
}
return 0;
}