Saving Beanstime limit:3000msmemory limit:32768kbthis problem would be judged onHDU. Original id:3037
64-bit integer IO format: %i64d Java class name: Main Although winter is far away, squirrels has to work day and night to save beans. They need plenty of food to get through those long cold days. After some time, the squirrel family thinks that they has to solve a problem. They suppose that they would save beans in n different trees. However, since the food was not sufficient nowadays, they would get no more than M beans. They want to know, how many ways there is to save no more than M beans (they is the same) in n trees.
Now they turn-to-do, you should give them the answer. The result may be extremely huge; You should output the result modulo p, because squirrels can ' t recognize large numbers.
InputThe first line contains one integer T, means the number of cases.
Then followed T lines, each line contains three integers n, m, p, means that squirrels would save no more than M same beans In n different trees, 1 <= N, M <= 1000000000, 1 < p < 100000 and p are guaranteed to be a prime.OutputYou should output the answer modulo p.Sample Input
21 2 52) 1 5
Sample Output
33
Solving the problem: Lucas asks the combination number to take the model
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4LL f[100010] = {1};5 voidinit (LL MoD) {6 for(inti =1; I <= mod; ++i)7F[i] = f[i-1]*i%MoD;8 }9ll gcd (ll a,ll b,ll &x,ll &y) {Ten if(!b) { Onex =1; Ay =0; - returnA; - } theLL ret = gcd (b,a%b,y,x); -Y-= x* (A/b); - returnret; - } + ll Inv (ll b,ll MoD) { -LL x,y,d =gcd (b,mod,x,y); + returnD = =1? (X%mod + MoD)%mod:-1; A } at LL INV (ll b,ll MoD) { - if(b = =1)return 1; - returnINV (MOD%B,MOD) * (mod-mod/b)%MoD; - } - ll Lucas (ll n,ll m,ll MoD) { -LL ret =1; in while(N &&m) { -LL A = n%MoD; toLL B = m%MoD; + if(A < b)return 0; -ret = RET*F[A]%MOD*INV (f[b]*f[a-b]%mod,mod)%MoD; theN/=MoD; *M/=MoD; $ }Panax Notoginseng returnret; - } the intMain () { + intKase,n,m,mod; Ascanf"%d",&Kase); the while(kase--) { +scanf"%d%d%d",&n,&m,&MoD); - init (mod); $printf"%i64d\n", Lucas (n+m,n,mod)); $ } - return 0; -}
View Code
HDU 3073 Saving Beans