Bzoj bzoj2982 Lucas theorem combination math
Question: I cannot review it after it is sent ...... In short, the general idea is to find C (n, m) mod 10007 m, n, [1, 2*10 ^ 8]
Lucas theorem: C (n, m) = C (n % P, M % P) * C (N/P, M/P) mod p requires P to be a prime number.
Where n % P may be less than M % P. In this case, 0 is returned directly.
Prove to ask Lucas. I don't know.
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define p 10007using namespace std;int fac[p],inv[p];void Linear_Shaker(){int i;fac[0]=1;for(i=1;i<p;i++)fac[i]=fac[i-1]*i%p;inv[1]=1;for(i=2;i<p;i++)inv[i]=(p-p/i)*inv[p%i]%p;inv[0]=1;for(i=1;i<p;i++)inv[i]=inv[i]*inv[i-1]%p;}int C(int n,int m){if(n<m)return 0;if(n<p&&m<p)return fac[n]*inv[m]%p*inv[n-m]%p;return C(n%p,m%p)*C(n/p,m/p)%p;}int main(){int T,n,m;Linear_Shaker();for(cin>>T;T;T--){scanf("%d%d",&n,&m);printf("%d\n",C(n,m));}}
Bzoj 2982 combination Lucas Theorem