Saving beans
Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1666 accepted submission (s): 592
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
Recommendgaojie
A very powerful number theory question cannot be pushed out, but fortunately, there are other people's ideas and we will push it again tomorrow to see if it can be launched,
However, there are a lot of knowledge points, for example, ferma's small theorem. For example, Lucas also calculates C (n, m)
These are basic algorithms, but I didn't know them before. I used them all. I was a cainiao but I learned the knowledge, I am still very happy.
Let's take a look at other people's problem-solving reports.
Then paste your code
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 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)
The result can be obtained by implementing the following Lucas theorem program. during the implementation process, pay attention to the forced conversion during multiplication.
#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;int N, M, P;int fac[100011];void factorial(){fac[0] = 1;for (int i = 1; i <= P; i++){fac[i] = (long long int)fac[i - 1] * i % P;}} int quick_pow(int a, int n){int ans = 1;while (n){if (n & 1){ans = (long long int)ans * a % P;}a = (long long int) a * a % P;n >>= 1;}return ans;}int C(int n, int m){if (n < m){return 0;}return (long long int)fac[n] * (quick_pow((long long int)fac[m] * fac[n - m] % P, P - 2)) % P;}int lucas(long long int n, long long int m){if (m == 0){return 1;}return (long long int)C(n % P, m % P) * lucas(n / P, m / P) % P;}int main(){int T;scanf("%d", &T);while (T--){scanf("%d%d%d", &N, &M, &P);factorial();int ans = lucas(N + M, M);printf("%d\n", ans);}//system("pause");return 0;}