[HDU 3461] Saving Beans & Combined Count Lucas theorem template

Source: Internet
Author: User

Saving Beans

Time limit:6000/3000 MS (java/others)

Memory limit:32768/32768 K (java/others)

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. SourceMulti-university Training Contest 13-host by hits" The Puzzle"Classic combination count, C (n+m,m) mod pThere is a famous theorem of Lucas theorem: C (n,m) mod p = C (n/p,m/p) *c (n%p,m%p)
1#include <bits/stdc++.h>2 using namespacestd;3 Long Longf[100005];4InlinevoidGetfact (Long Longp) {5f[0]=1;6      for(intI=1; i<=p;++i)7F[i]= (f[i-1]*i)%p;8 }9InlineLong LongPOWX (Long LongALong LongBLong Longp) {Ten     Long Longret=1; One      while(b) { A         if(b&1) ret= (ret*a)%p; -A= (a*a)%p; -b>>=1; the     } -     returnret; - } -InlineLong LongLucasLong LongNLong LongMLong Longp) { +     Long Longret=1; -      while(n&&m) { +         Long Longa=n%p,b=m%p; A         if(A&LT;B)return 0; atRet= (Ret*f[a]*powx (f[b]*f[a-b)%p,p-2, p))%p; -N/=p;m/=p; -     } -     returnret; - } - intMain () { in     intT; -scanf"%d",&t); to      while(t--) { +         Long Longn,m,p; -scanf"%i64d%i64d%i64d",&n,&m,&p); the getfact (p); *printf"%i64d\n", Lucas (n+m,m,p)); $     }Panax Notoginseng     return 0; -}
View Code

[HDU 3461] Saving Beans & Combined Count Lucas theorem template

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.