hdu 3037 Saving Beans (Lucas)
来源:互联网 发布:windows激活工具有哪些 编辑:程序博客网 时间:2024/06/05 04:36
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
2
1 2 5
2 1 5
Sample Output
3
3
代码
#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;ll t, n, m, p;ll exgcd(ll a, ll b, ll &x, ll &y) { if(! b) { x = 1, y = 0; return a; } ll xx, yy; ll r = exgcd(b, a % b, xx, yy); x = yy; y = xx - (a / b) * yy; return r;} ll inverse(ll a, ll m) { ll d, x, y; d = exgcd(a, m, x, y); return (x % m + m) % m;}ll mul(ll a) { ll sum = 1; for(ll i = a; i >= 2; i --) sum = sum * i % p; return sum % p;}ll c(ll n, ll m) { if(m > n) return 0; ll x = mul(m), y = mul(n - m); ll z = x * y % p; z = inverse(z, p); ll ans = (mul(n) % p * (z % p)) % p; return ans;}ll lucas(ll n, ll m) { if(m > n) return 0; if(n / p <= 1) return c(n % p, m % p) % p; return ((lucas(n / p, m / p) % p) * (c(n % p, m % p) % p)) % p;}int main() { scanf("%lld", &t); while(t --) { scanf("%lld %lld %lld", &n, &m, &p); printf("%lld\n", lucas(n + m, m)); } return 0;}
- 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的套用)
- [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定理)
- STM8L151系列单片机PWM配置时注意事项
- WEB前段快速入门到玩溜
- 一个分布式测试系统利器
- 全连结神经网络实现mnist字符识别
- Linux下查看进程打开的文件句柄数
- hdu 3037 Saving Beans (Lucas)
- 谈谈ObjectiveC中__block
- SQL SERVER与C#数据类型对照表
- 李炎恢的bootstrap项目实战首页内容下思路解析
- LightOJ-1002-Country Roads [最短路][Dijkstra]
- Tracert 抓包测试
- HDU 1829 A Bug's Life(带权并查集)
- 实现RxBus代替EventBus
- I