Saving Beans

Time Limit: 3000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3037
64-bit integer IO format: %I64d      Java class name: Main

 
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 解题:Lucas 求组合数取模
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL F[] = {};
void init(LL mod) {
for(int i = ; i <= mod; ++i)
F[i] = F[i-]*i%mod;
}
LL gcd(LL a,LL b,LL &x,LL &y) {
if(!b) {
x = ;
y = ;
return a;
}
LL ret = gcd(b,a%b,y,x);
y -= x*(a/b);
return ret;
}
LL Inv(LL b,LL mod) {
LL x,y,d = gcd(b,mod,x,y);
return d == ?(x%mod + mod)%mod:-;
}
LL inv(LL b,LL mod) {
if(b == ) return ;
return inv(mod%b,mod)*(mod-mod/b)%mod;
}
LL Lucas(LL n,LL m,LL mod) {
LL ret = ;
while(n && m) {
LL a = n%mod;
LL b = m%mod;
if(a < b) return ;
ret = ret*F[a]%mod*Inv(F[b]*F[a-b]%mod,mod)%mod;
n /= mod;
m /= mod;
}
return ret;
}
int main() {
int kase,n,m,mod;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d%d",&n,&m,&mod);
init(mod);
printf("%I64d\n",Lucas(n+m,n,mod));
}
return ;
}

HDU 3073 Saving Beans的更多相关文章

  1. hdu 3037 Saving Beans(组合数学)

    hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...

  2. hdu 3037 Saving Beans Lucas定理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. hdu 3037 Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3037——Saving Beans

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. Hdu 3037 Saving Beans(Lucus定理+乘法逆元)

    Saving Beans Time Limit: 3000 MS Memory Limit: 32768 K Problem Description Although winter is far aw ...

  6. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  7. HDU 3037 Saving Beans (Lucas法则)

    主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...

  8. HDU 3037 Saving Beans(Lucas定理的直接应用)

    解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...

  9. HDU 3037 Saving Beans (数论,Lucas定理)

    题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...

随机推荐

  1. ZOJ 2316 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on ZJU. O ...

  2. POJ 3737

    第一道三分题,有模板 #define eps 10e-6 double cal(){}//计算题目所需要的值 while(l+eps<r) { m1=l+(r-l)/3; m2=r-(r-l)/ ...

  3. 关于synchronized与volatile的一点认识

    贪婪是一种原罪,不要再追求性能的路上离正确越来越远. 内存模型 java内存模型 pageId=27903261#%E5%85%B3%E4%BA%8Esynchronized%E4%B8%8Evola ...

  4. 【手势交互】6. 微动VID

    中国 天津 http://www.sharpnow.com/ 微动VID是天津锋时互动科技有限公司开发的中国Leap Motion. 它能够识别并跟踪用户手部的姿态.包含:指尖和掌心的三维空间位置:手 ...

  5. 图像滤镜艺术---保留细节的磨皮之C#程序实现

    上一篇博文"保留细节的磨皮滤镜之PS实现"一文中.我简介了本人自己总结的一种非常easy的磨皮滤镜,这个滤镜在磨光皮肤的同一时候,会保留非常不错的细节,今天,我将介绍使用C#程序实 ...

  6. NYOJ_94 cigarettes 递归VS迭代

    题目地址 分析: 英文题事实上看懂意思和正常的也都差点儿相同.就算有几个单词不认识也无伤大雅. 一共同拥有n支烟,每天抽k支. 每抽完k支,会得到一仅仅. a组数据.  输入n k的个数.输出一共抽了 ...

  7. 从零单排入门机器学习:线性回归(linear regression)实践篇

    线性回归(linear regression)实践篇 之前一段时间在coursera看了Andrew ng的机器学习的课程,感觉还不错,算是入门了. 这次打算以该课程的作业为主线,对机器学习基本知识做 ...

  8. HTML5 Canvas 获取网页的像素值。

    我之前在网上看过一个插件叫做出JScolor   颜色拾取器  说白了就是通过1*1PX的DOM设置颜色值通过JS来获取当前鼠标点击位置DOM的颜色值. 自从HTML5 画布出来之后.就有更好的方法来 ...

  9. DB-MySQL:MySQL 复制表

    ylbtech-DB-MySQL:MySQL  复制表 1.返回顶部 1. MySQL 复制表 如果我们需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TA ...

  10. Linux 安装Redis 5.0

    结构如下: Redis 官方不建议Redis安装在WINDOWS 服务器上(尤其是生产中分布式事物缓存). linux 下Redis 5.0主从复制(一主二从)哨兵模式的搭建:https://www. ...