题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982
$C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\% P$
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int mod=1e4+7; 6 int inv[10010],fac[10010]; 7 int C(int x,int y){ 8 if(x<y) return 0; 9 return fac[x]*inv[fac[y]]%mod*inv[fac[x-y]]%mod; 10 } 11 int main(){ 12 inv[1]=1;for(int i=2;i<10007;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; 13 fac[0]=1;for(int i=1;i<10007;i++) fac[i]=fac[i-1]*i%mod; 14 int Test; 15 scanf("%d",&Test); 16 while(Test--){ 17 int N,M; 18 scanf("%d%d",&N,&M); 19 if(N<M){ 20 puts("0"); 21 continue; 22 } 23 int Ans=1; 24 while(M){ 25 Ans=Ans*C(N%mod,M%mod)%mod; 26 N/=mod; 27 M/=mod; 28 } 29 printf("%d\n",Ans); 30 } 31 return 0; 32 }