首页 > 代码库 > hdu 3037Saving Beans(卢卡斯定理)
hdu 3037Saving Beans(卢卡斯定理)
Saving Beans
Saving Beans
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5761 Accepted Submission(s): 2310
Problem Description
Although 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 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.
Now they turn to you for help, 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.
Input
The 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 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.
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.
Output
You should output the answer modulo p.
Sample Input
21 2 52 1 5
Sample Output
33
Hint
HintFor 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.
Source
2009 Multi-University Training Contest 13 - Host by HIT
/*题目相当于求n个数的和不超过m的方案数。如果和恰好等于m,那么就等价于方程x1+x2+...+xn = m的解的个数,利用插板法可以得到方案数为:(m+1)*(m+2)...(m+n-1) = C(m+n-1,n-1) = C(m+n-1,m)现在就需要求不大于m的,相当于对i = 0,1...,m对C(n+i-1,i)求和,根据公式C(n,k) = C(n-1,k)+C(n-1,k-1)得C(n-1,0)+C(n,1)+...+C(n+m-1,m)= C(n,0)+C(n,1)+C(n+1,2)+...+C(n+m-1,m)= C(n+m,m)现在就是要求C(n+m,m) % p,其中p是素数。然后利用Lucas定理的模板就可以轻松的求得C(n+m,m) % p的值*/#include<iostream>#include<cstdio>#include<cstring>#define N 100007using namespace std;long long f[N];long long Mi(long long a,long long b,long long p){ long long res=1; while(b) { if(b&1) res=res*a%p; b>>=1;a=a*a%p; }return res;}long long C(long long n,long long m,long long p){ if(m>n)return 0; return f[n]*Mi(f[m]*f[n-m]%p,p-2,p)%p;}long long Lcs(long long n,long long m,long long p){ if(m==0)return 1; return (C(n%p,m%p,p)*Lcs(n/p,m/p,p))%p;}int main(){ long long n,m,p;long long t; cin>>t; while(t--) { cin>>n>>m>>p; f[0]=1; for(long long i=1;i<=p;i++) f[i]=f[i-1]*i%p; printf("%lld\n",Lcs(n+m,m,p)); } return 0;}
hdu 3037Saving Beans(卢卡斯定理)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。