HDU 3037:Saving Beans Lucas模板题+乘法逆元

来源:互联网 发布:linux系统制作iso镜像 编辑:程序博客网 时间:2024/05/17 14:30

Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3489    Accepted Submission(s): 1327


Problem Description
Although 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 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.
 

Input
The 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 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.
 

Output
You should output the answer modulo p.
 

Sample Input
21 2 52 1 5
 

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

题意是从n个树中摘不超过m个果子,有多少种不同的摘法。

实际上就是求x1+x2+x3...+xn <= m有多少种不同的解。插板法,插第一个板子有m+1中方法,第二个板子有m+2种方法,一直插了n个板子。最后还要除以相同的数量,就是n!。其结果就是很裸的C[n+m][n]。

Lucas定理,我的理解就是当组合数很大,并且求的结果是取模p 运算的时候,C[n][m]其中的n在P进制下表示为n[0]n[1]...n[x],m同样可以表示为m[0]m[1]...m[x]。那么有C[n][m]%p=C[n[0]][m[0]]*C[n[1]][m[1]]*C[n[2]][m[2]]...*C[n[x]][m[x]]%p。

这样我们求C[n][m]的时候 就可以直接表示为Lucas(n,m,p)=C[n%p][m%p]*Lucas(n/p,m/p,p)。

然后这个题目在求C[n%p][m%p]因为p最大是10^5,依然还是很大,求C[n][m]%p的时候是等于n!/((n-m)!*m!)%p,除以(n-m)!*m!再取模p,相当于乘以(n-m)!*m!模p的逆元。

这时,因为p肯定是一个质数,所以根据费马小定理有 a^(p-1)=1(mod p)。因此有a*a^(p-2)=1(mod p)。由逆元的定义可知,a^(p-2)就是a模p的逆元。也就是(n-m)!*m! 求它的(p-2)次方。

代码:

#pragma warning(disable:4996)  #include <iostream>  #include <algorithm>  #include <cmath>  #include <vector>  #include <string>  #include <cstring>#include <queue>using namespace std;typedef long long LL;LL exp_mod(LL a, LL b, LL p){LL res = 1;while (b != 0) {if (b & 1)res = (res * a) % p;a = (a*a) % p;b >>= 1;}return res;}LL Comb(LL a, LL b, LL p) {if (a < b)   return 0;if (a == b)  return 1;if (b > a - b)  b = a - b;LL ans = 1, ca = 1, cb = 1;for (LL i = 0; i < b; ++i){ca = (ca * (a - i)) % p;cb = (cb * (b - i)) % p;}ans = (ca*exp_mod(cb, p - 2, p)) % p;return ans;}LL Lucas(LL n, LL m, LL p) {LL ans = 1;while (n&&m&&ans) {ans = (ans*Comb(n%p, m%p, p)) % p;n /= p;m /= p;}return ans;}int main(){LL n, m, p;int t;scanf("%d", &t);while (t--){scanf("%lld%lld%lld", &n, &m, &p);printf("%lld\n", Lucas(n + m, n, p));}return 0;}


0 0
原创粉丝点击