poj——|poj—— 3037 Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5769Accepted Submission(s): 2316
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 2 1 2 5 2 1 5
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.【poj——|poj—— 3037 Saving Beans】
Source 2009 Multi-University Training Contest 13 - Host by HIT
Recommend gaojie|We have carefully selected several similar problems for you:30333038303630353034题意:在n树中有多少种方式可以节省不超过m豆子(他们是一样的)。 思路: 题目可以转换成x1+x2+……+xn=m 有多少组解,m在题中可以取0~m。
利用插板法可以得出x1+x2+……+xn=m解的个数为C(n+m-1,m);
则题目解的个数可以转换成求sum=C(n+m-1,0)+C(n+m-1,1)+C(n+m-1,2)……+C(n+m-1,m)
利用公式C(n,r)=C(n-1,r)+C(n-1,r-1)== >sum=C(n+m,m)。
就是要求C(n+m,m)%p。
代码:
#include#include #include #include #include #define ll long long using namespace std; ll t,n,m,p,ans; ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar(); } return x*f; } ll qpow(ll n,ll k) { ll res=1; while(k) { if(k&1) res=res*n%p; n=n*n%p; k>>=1; }return res; } ll c(ll n,ll m) { if(m>n) return 0; ll n1=1,m1=1; for(ll i=n-m+1; i<=n; i++) n1=n1*i%p; for(ll i=1; i<=m; i++) m1=m1*i%p; return n1*qpow(m1,p-2); } ll lus(ll n,ll m) { if(m==0) return 1; return c(n%p,m%p)*lus(n/p,m/p)%p; } int main() { t=read(); while(t--) { n=read(),m=read(),p=read(); ans=lus(n+m,m); printf("%lld\n",ans); } return 0; }
转载于:https://www.cnblogs.com/z360/p/7360435.html
推荐阅读
- 急于表达——往往欲速则不达
- 慢慢的美丽
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 2019-02-13——今天谈梦想()
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- Ⅴ爱阅读,亲子互动——打卡第178天
- 低头思故乡——只是因为睡不着
- 取名——兰
- 每日一话(49)——一位清华教授在朋友圈给大学生的9条建议
- 广角叙述|广角叙述 展众生群像——试析鲁迅《示众》的展示艺术