标签:
Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long f[100005]; 4 inline void getfact(long long p) { 5 f[0]=1; 6 for (int i=1;i<=p;++i) 7 f[i]=(f[i-1]*i)%p; 8 } 9 inline long long powx(long long a,long long b,long long p) { 10 long long ret=1; 11 while(b) { 12 if (b&1) ret=(ret*a)%p; 13 a=(a*a)%p; 14 b>>=1; 15 } 16 return ret; 17 } 18 inline long long lucas(long long n,long long m,long long p) { 19 long long ret=1; 20 while(n&&m) { 21 long long a=n%p,b=m%p; 22 if(a<b) return 0; 23 ret=(ret*f[a]*powx((f[b]*f[a-b])%p,p-2,p))%p; 24 n/=p;m/=p; 25 } 26 return ret; 27 } 28 int main() { 29 int t; 30 scanf("%d",&t); 31 while(t--) { 32 long long n,m,p; 33 scanf("%I64d%I64d%I64d",&n,&m,&p); 34 getfact(p); 35 printf("%I64d\n",lucas(n+m,m,p)); 36 } 37 return 0; 38 }
[HDU 3461] Saving Beans & 组合计数Lucas定理模板
标签:
原文地址:http://www.cnblogs.com/TonyNeal/p/hdu3461.html