Suppose there are n + 1 trees, n + 1 trees are buried less than m seeds, partition method C [n + M] [m]
The Lucas theorem is used to obtain the number of major groups in mod:
Lucas (n, m, p) = C [n % P] [M % P] × Lucas (N/P, M/P, P );
Saving beans
Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2314 accepted submission (s): 845
Problem descriptionalthough winter is far away, squirrels have 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 have to solve a problem. they suppose that they will save beans in n different trees. however, since the food is not sufficient nowadays, they will get no more than m beans. they want to know that how many ways there are to save no more than m beans (they are the same) in N trees.
Now they turn to you for help, you shocould give them the answer. The result may be extremely huge; You shocould output the result modulo p, because squirrels can't recognize large numbers.
Inputthe first line contains one integer T, means the number of instances.
Then followed t lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, M <= 1000000000, 1 <p <100000 and P is guaranteed to be a prime.
Outputyou shoshould output the answer modulo p.
Sample Input
21 2 52 1 5
Sample output
33HintHintFor sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2 … and so on. The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are: put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.
Source2009 multi-university training contest 13-host by hit
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long int LL;LL n,m,p;LL fact[100100];LL QuickPow(LL x,LL t,LL m){if(t==0) return 1LL;LL e=x,ret=1LL;while(t){if(t&1) ret=(ret*e)%m;e=(e*e)%m;t>>=1LL;}return ret%m;}void get_fact(LL p){fact[0]=1LL;for(int i=1;i<=p+10;i++)fact[i]=(fact[i-1]*i)%p;}LL Lucas(LL n,LL m,LL p){///lucas(n,m,p)=c[n%p][m%p]*lucas(n/p,m/p,p);LL ret=1LL;while(n&&m){LL a=n%p,b=m%p;if(a<b) return 0;ret=(ret*fact[a]*QuickPow((fact[b]*fact[a-b])%p,p-2,p))%p;n/=p; m/=p;}return ret%p;}int main(){int T_T;scanf("%d",&T_T);while(T_T--){LL n,m,p;cin>>n>>m>>p;get_fact(p);cout<<Lucas(n+m,m,p)<<endl;}return 0;}