hdu 3037 Saving Beans(组合数学+lucas定理)

来源:互联网 发布:复杂网络的混沌同步 编辑:程序博客网 时间:2024/05/18 23:11

Saving Beans

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


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
Hint

Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2823    Accepted Submission(s): 1063
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.
 

Source
2009 Multi-University Training Contest 13 - Host by HIT
 
For 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.
 

Source
2009 Multi-University Training Contest 13 - Host by HIT
题目分析:
首先这道题要求组合数的前缀和,那么也就是从n个选0-m个的方案数,那么我们可以换一种思路这样想,就是从n+m个物品中选m个,有m个物品无效,那么也相当于从剩下的那n个物品中选0-m个的方案数,然后就是求解这个庞大的组合数的方法了,首先取模的数是一个素数,那么利用lucas定理可知C(n/p,m/p)*C(n%p,m%p)%p = C(n,m)% p,也就是可以利用min(n,m)/p的复杂度将C化为p以内的组合数,那么求这个组合数,我们可以预处理出p以内的全部阶乘,然后利用n!/(m!*(n-m)!)计算,注意求逆元,因为取模的数是个素数,所以可以利用欧拉定理求逆元
因为a^phi(i) = 1(mod i),如果i是个素数的话,那么相当于a^(p-1)=1(mod p),也就是a^(p-2)%p就是a的逆元
那么到这里这道题就是一道大水题了。。。。。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define MAX 100007using namespace std;typedef long long LL;int t;LL n,m,p;LL f[MAX];LL pow (  LL a , LL n , LL p ){    LL sum = 1;    while ( n )    {        if ( n&1 ) sum =  sum*a%p;        a = a*a%p;        n >>= 1;     }    //cout << " sum : " << sum << endl;    return sum;}void init ( int p){    f[1] = f[0] = 1;    for ( LL i = 2 ; i <= p; i++ )        f[i] = f[i-1]*i%p;}LL C ( LL a , LL b ){    return f[a]*pow ( f[b]*f[a-b]%p , p-2 , p )%p;}LL Lucas ( LL a , LL b , LL p ){    LL ret = 1;    while ( a&&b )    {        LL n = a%p , m = b%p;        if ( n < m ) return 0;        ret = ret * C ( n , m )%p;        a /= p;        b /= p;    }    return ret;}int main ( ){    scanf ( "%d" , &t );    while ( t-- )    {        scanf ( "%lld%lld%lld" , &n , &m , &p );        init( p );        printf ( "%lld\n" , Lucas ( n+m , m , p ) );    }}   


0 0
原创粉丝点击