[ACM] HDU 3037 saving beans (Lucas theorem, modulo of composite number)

Source: Internet
Author: User

Saving beans

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 refers to the total number of methods to place no more than m beans on N different trees.

The following is reproduced in:

Http://blog.csdn.net/tju_virus/article/details/7843248

The question is equivalent to the number of N numbers and the number of solutions that do not exceed M.

If the sum is exactly equal to m, it is equivalent to the number of solutions of the equation X1 + X2 +... + Xn = m. The number of solutions can be obtained using the plug-in method:

(M + 1) * (m + 2)... (m + N-1) = C (m + n-1, n-1) = C (m + n-1, m)

Now we need to require a value not greater than m, which is equivalent to I = 0, 1 ..., M for C (n + I-1, I) summation, according to the formula C (n, k) = C (n-1, k) + C (n-1, k-1) obtained

C (n-1, 0) + C (n, 1) +... + C (n + m-1, m)

= C (n, 0) + C (n, 1) + C (n +) +... + C (n + s-1, m)

= C (n + M, m)

Now C (n + M, m) % P is required, where p is a prime number.

Then, we can use the template of the Lucas theorem to easily obtain the value of C (n + M, m) % P.


The following is a brief introduction to the Lucas theorem:

The Lucas theorem is used to evaluate the value of C (n, m) mod P, and P is a prime number (M combination from N, P on the modulo ).
Description:
Lucas (n, m, p) = C (n % P, M % P) * Lucas (N/P, M/P, P)
Lucas (x, 0, P) = 1;

A and B are non-negative integers, and P are prime numbers. AB is written in P Notation: A = A [n] a [n-1]... A [0], B = B [N] B [n-1]... B [0].

Then, the combination numbers C (A, B) and C (A [n], B [N]) * C (A [n-1], B [n-1]) *... * C (A [0], B [0]) modp same

That is, Lucas (n, m, p) = C (n % P, M % P) * Lucas (N/P, M/P, P)

A simple understanding is:

To solve n! % P is used as an example to segment n, where each P segment returns the same result. However, we need to separate P, 2 p,... at the end of each segment and extract P. We will find that the remaining number is exactly (N/P )! , Equivalent

This is a subproblem.

This is to process N separately! Of course C (n, m) is n! /(M! * (N-m )!), If each factorial is processed using the above method, it is the Lucas theorem.

Lucas's maximum data processing capability is P at around 10 ^ 5.

C (a, B) =! /(B! * (A-B )! ) Mod p

In fact, it is to (! /(A-B )!) * (B! ) ^ (P-2) mod p

(The above transformation is based on the Fermat's theorem: If P is a prime number and a and P are mutually qualitative, then the remainder of P-1 is 1,

Then, if a and a ^ (P-2) are reciprocal multiplication inverse elements, (B/A) = (B * a ^ (P-2) mod P)


Code:

# Include <iostream> # include <string. h> # include <cmath> # define ll long longusing namespace STD; const int maxn = 100002; ll n, m, p; ll FAC [maxn]; void getfac (ll p) // preprocessing class {FAC [0] = 1; for (INT I = 1; I <= P; I ++) FAC [I] = FAC [I-1] * I % P;} ll power (LL A, ll N, ll p) // Fast Power Operation {ll ans = 1; while (n) {If (N & 1) ans = ans * A % P; A = A * A % P; N/= 2;} return ans ;} ll Lucas (ll n, ll M, ll p) {ll ans = 1; while (N & M) {ll A = n % P; LL B = m % P; if (A <B) Return 0; ans = (ANS * FAC [a] * power (FAC [B] * FAC [A-B] % P, P-2, p )) % P; // FAC [B] * FAC [A-B] Do not forget % P, otherwise wa N/= P; M/= P;} return ans ;} int main () {int t; CIN> T; while (t --) {CIN> N> m> P; getfac (P ); cout <Lucas (n + M, m, p) <Endl;} return 0 ;}

There are two ways to write the Lucas theorem:

Ll Lucas (ll n, ll M, ll p) {ll ans = 1; while (N & M) {ll A = n % P; LL B = m % P; if (A <B) Return 0; ans = (ANS * FAC [a] * power (FAC [B] * FAC [A-B] % P, P-2, p )) % P; // FAC [B] * FAC [A-B] Do not forget % P, otherwise wa N/= P; M/= P;} return ans ;}

int Lucas(lld n,lld m,lld p)  {      if(m==0)          return 1;      return((lld)Cm(n%p,m%p,p)*(lld)Lucas(n/p,m/p,p))%p;  } 
Int cm (lld n, lld m, LLD p) {lld a = 1, B = 1; if (M> N) return 0; // implement (! /(A-B )!) * (B !) ^ (P-2) mod P, because N is relatively large, I do not know any good optimization here while (m) {A = (A * n) % P; B = (B * m) % P; m --; n --;} return (LLD) A * (LLD) Pow (B, P-2, p) % P ;}






[ACM] HDU 3037 saving beans (Lucas theorem, modulo of composite number)

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.