HDU 3037 Saving Beans Lucas theorem

Source: Internet
Author: User

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 Descriptionalthough 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 Input21 2 52 1 5

Sample Output33 HintHintfor Sample 1, squirrels would put no more than 2 beans in one tree. Since trees is 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 beans in tree 1 and put 1 beans in tree 2.

Source2009 multi-university Training Contest 13-host by hits

Naked Lucas theorem, the function can be called directly.

I don't understand why C ((n+m), m) for the time being, let's study it later.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespaceStd;typedefLong LongLl;ll Quick_mod (ll a,ll b,ll m) {ll ans=1; A%=m;  while(b) {if(b&1) ans= ans * a%m; b>>=1; A= A * A%m; }    returnans;} ll GetC (ll N, ll m,ll MoD) {if(M >N)return 0; if(M > Nm) m= Nm; ll a=1, B =1;  while(M) {a= (a*n)%MoD; b= (b*m)%MoD; M--; N--; }    returnA*quick_mod (b,mod-2, MoD)%MoD;} ll Lucas (ll n,ll k,ll MoD) {if(k = =0)        return 1; returnGetC (N%mod,k%mod,mod) *lucas (n/mod,k/mod,mod)%MoD;}intMain () {intT; scanf ("%d",&T);  while(t--) {ll n,m,mod; scanf ("%lld%lld%lld",&n,&m,&MoD); printf ("%lld\n", Lucas (n+m,m,mod)); }    return 0;}
View Code

HDU 3037 Saving Beans Lucas theorem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.