Hdu3037 Saving Beans (Lucas theorem + ferma's theorem or extends Euclidean Algorithm), hdu3037lucas

Source: Internet
Author: User

Hdu3037 Saving Beans (Lucas theorem + ferma's theorem or extends Euclidean Algorithm), hdu3037lucas
Saving BeansTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3221 Accepted Submission (s): 1234


Problem DescriptionAlthough 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 shocould give them the answer. The result may be extremely huge; you shocould output the result modulo p, because squirrels can't recognize large numbers.
InputThe first line contains one integer T, means the number of instances.

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.
OutputYou shoshould output the answer modulo p.
Sample Input

21 2 52 1 5

Sample Output
33HintHintFor 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. 

Source2009 Multi-University Training Contest 13-Host by HIT
Question: Not greaterM seeds are stored in n trees and asked how many methods are available (note that the value is not greater than or equal ).

Analysis: first, there are no more than m seeds. I can't think that the seeds smaller than m are stored on the n + 1 tree. In this case, the problem is transformed into the number of solutions to store m seeds on n + 1 trees. OK, this is the formula in the combination of mathematics, that is, the plug-in method, that is, X1 + X2 + X3 + ...... + Xn + 1 = m; so, the answer is C (n + m, m ). Then the basic ferma's theorem is used to calculate the inverse element (of course, we can also use the Extended Euclidean Algorithm to obtain the inverse element. However, I just started to study number theory and won't-). The Lucas theorem is solved.

I don't know ferma's theorem.Click here;

Extends the Euclidean algorithm.Click here;

Lucas's theorem does not understandClick here.


# Include <iostream> # include <cstdio> # include <cstring> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <cmath> # include <algorithm> using namespace std; const double eps = 1e-6; const double pi = acos (-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; # define ll long # define CL () memset (a, 0, sizeof (a) ll f [100010]; ll init (ll p) // returns a factorial for calculating the number of combinations {f [0] = 1; for (int I = 1; I <= p; I ++) f [I] = f [I-1] * I % p;} ll mod_pow (int a, int n, int p) // here the result is a ^ n, that is (f [bb] * f [aa-bb] % p) ^ (P-2) {ll aa =, re = 1; while (n) {if (n & 1) re = (re * aa) % p; aa = (aa * aa) % p; n> = 1;} return re;} ll Lucas (ll a, ll B, ll p) // Lucas theorem {ll re = 1; while (a & B) {ll aa = a % p, bb = B % p; if (aa <bb) return 0; re = re * f [aa] * mod_pow (f [bb] * f [aa-bb] % p, P-2, p) % p; // returns reverse Element a/= p; B/= p;} return re;} int main () {int T; ll n, m, p; cin> T; while (T --) {cin> n> m> p; init (p); cout <Lucas (n + m, m, p) <endl;} return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.