标签:des style blog class code java
Time Limit: 6000/3000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 2079 Accepted
Submission(s): 748
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<vector> 6 using namespace std; 7 typedef __int64 LL; 8 9 LL dp[100002]; 10 11 void init(LL p){ 12 int i; 13 dp[0]=1; 14 for(i=1;i<=p;i++) 15 dp[i]=(dp[i-1]*i)%p; 16 } 17 LL pow_mod(LL a,LL n,LL p) 18 { 19 LL ans=1; 20 while(n) 21 { 22 if(n&1) ans=(ans*a)%p; 23 n=n>>1; 24 a=(a*a)%p; 25 } 26 return ans; 27 } 28 LL C(LL a,LL b,LL p) 29 { 30 if(a<b) return 0; 31 if(b>a-b) b=a-b; 32 LL sum1=dp[a]; 33 LL sum2=(dp[b]*dp[a-b])%p; 34 sum1=(sum1*pow_mod(sum2,p-2,p)); 35 return sum1; 36 } 37 LL Lucas(LL n,LL m,LL p) 38 { 39 LL ans=1; 40 while(n&&m&&ans) 41 { 42 ans=(ans*C(n%p,m%p,p))%p; 43 n=n/p; 44 m=m/p; 45 } 46 return ans; 47 } 48 int main() 49 { 50 int T; 51 LL n,m,p; 52 scanf("%d",&T); 53 while(T--) 54 { 55 scanf("%I64d%I64d%I64d",&n,&m,&p); 56 init(p); 57 if(n>m)swap(n,m); 58 LL ans= Lucas(n+m,m,p); 59 printf("%I64d\n",ans); 60 } 61 return 0; 62 }
hdu 3037 Saving Beans,布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/tom987690183/p/3705763.html