[ACM] hdu 3037 Saving Beans (Lucas theorem, modulo of composite number)

Source: Internet
Author: User

[ACM] hdu 3037 Saving Beans (Lucas theorem, modulo of composite number)

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
 
  
# Include
  
   
# Include
   
    
# 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 (
    
     
> T; while (t --) {cin> n> m> p; getfac (p); cout <
     
      
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 (
       
        
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 ;}






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.