标签:saving may integer iss define 为什么 lin mode get
Saving Beans
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4315 Accepted Submission(s): 1687
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.
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.
Output
You should output the answer modulo p.
Sample Input
Sample Output
3 3
Hint
Hint For 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
题目大意:把不超过m颗种子放到n颗树上的方案数。
哼!一直不明白为什么是C(n+m,m),网上的题解我也看不懂...(垃圾
好久终于明白了。ono
题解:假设就是m颗种子,那么原题就相当于
x1+x2+x3+x4+....+xn=m
然后用插板法我们知道,方案数为C(m-1,n-1)。
但是题目是不超过m,那么就是把0颗豆子放到n个树,1颗豆子放到n个树,2
颗豆子...m颗豆子。
那么我们再造一颗树标号为n+1,那么如果把k个豆子放入n颗树,然后再把m-k个
豆子放入第n+1颗树。
因为树可以为空,所以我们设b[i]=x[i]+1,
因为x[1]+x[2]+x[3]+...+x[n+1]=m,
那么b[1]+b[2]+b[3]+...+b[n+1]=m+n+1..
那么就相当于把n个挡板放入m+n个空中,就是C(m+n,n)=C(m+n,m)。
代码:hdu炸了没测
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
int T;
LL n,m,p,f[100009];
void pre(){
f[0]=1;
for(int i=1;i<=p;i++)f[i]=f[i-1]*i%p;
}
LL ksm(LL x,LL y){
LL ret=1%p;
while(y){
if(y&1)ret=ret*x%p;
x=x*x%p;
y>>=1;
}
return ret;
}
LL C(LL n,LL m){
if(m>n)return 0;//*******
return f[n]*ksm(f[m],p-2)%p*ksm(f[n-m],p-2)%p;
}
LL Lucas(LL n,LL m){
if(!m)return 1;
return C(n%p,m%p)*Lucas(n/p,m/p)%p;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%lld%lld%lld",&n,&m,&p);
pre();
printf("%lld\n",Lucas(n+m,m)%p);
}
return 0;
}
hdu 3037 Saving Beans
标签:saving may integer iss define 为什么 lin mode get
原文地址:http://www.cnblogs.com/zzyh/p/7668043.html