[BZOJ2982]combination Lucas定理

时间:2022-02-11 22:13:17

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982

$C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\% P$

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1e4+;
int inv[],fac[];
int C(int x,int y){
if(x<y) return ;
return fac[x]*inv[fac[y]]%mod*inv[fac[x-y]]%mod;
}
int main(){
inv[]=;for(int i=;i<;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
fac[]=;for(int i=;i<;i++) fac[i]=fac[i-]*i%mod;
int Test;
scanf("%d",&Test);
while(Test--){
int N,M;
scanf("%d%d",&N,&M);
if(N<M){
puts("");
continue;
}
int Ans=;
while(M){
Ans=Ans*C(N%mod,M%mod)%mod;
N/=mod;
M/=mod;
}
printf("%d\n",Ans);
}
return ;
}