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.
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.
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
33HintHintSaving Beans
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2823 Accepted Submission(s): 1063Problem 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 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.InputThe 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.OutputYou should output the answer modulo p.Sample Input21 2 52 1 5Sample Output33HintHintFor 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 HITFor 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的逆元
那么到这里这道题就是一道大水题了。。。。。
首先这道题要求组合数的前缀和,那么也就是从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
- hdu 3037 Saving Beans(组合数学+lucas定理)
- HDU 3037 Saving Beans(组合数学+Lucas定理)
- hdu 3037 Saving Beans 【大组合数取模-Lucas定理+逆元取模】
- 大组合数取模 hdu 3037 Saving Beans lucas定理
- [ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)
- HDU 3037 Saving Beans 大组合数 lucas定理
- hdu 3037 Saving Beans (大组合数取模--Lucas定理)
- hdu-3037-Saving Beans(Lucas定理+大组合数取模)
- HDU 3037 Saving Beans 组合数取模 Lucas定理
- HDU - 3037 Saving Beans (数论,组合数取模,lucas定理)
- hdu 3037 Saving Beans(Lucas定理)
- HDU 3037 Saving Beans (Lucas定理)
- HDU 3037 Saving Beans(lucas定理)
- HDU 3037 Saving Beans (Lucas定理)
- [Lucas定理] hdu 3037 Saving Beans
- hdu 3037 saving beans (lucas定理)
- HDU 3037 Saving Beans (Lucas定理)
- hdu 3037 Saving Beans lucas定理
- BlockingQueue、PriorityBlockingQueue
- 如何通过JQuery将DIV的滚动条滚动到指定的位置
- 登录验证码的实现
- UVA11235:Frequent values(RMQ)
- leetcode--BinaryTreeInorderTraversal
- hdu 3037 Saving Beans(组合数学+lucas定理)
- 光流法(Optical Flow)
- Android即时聊天系统--随聊APP之接口定义
- sencha grid templatecolumn模板列和actioncolumn
- Java关键字final
- unity3d easytouch教程
- 学习HTML5 canvas第一天
- Maven最佳实践
- RedHat5安装gstreamer过程记录