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.
 
题目大意:求C(n + m, m) % p
思路:http://blog.csdn.net/acm_cxlove/article/details/7844973
 
代码(1234MS):
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ; int fac[MAXN];
int n, m, p, T; int power(int x, int p, int mod) {
int res = ;
while(p) {
if(p & ) res = (LL)res * x % mod;
x = (LL)x * x % mod;
p >>= ;
}
return res;
} void init_fac(int p) {
fac[] = ;
for(int i = ; i <= p; ++i)
fac[i] = (LL)fac[i - ] * i % p;
} int lucas(int n, int m, int p) {
int res = ;
while(n && m) {
int a = n % p, b = m % p;
if(a < b) return ;
res = (LL)res * fac[a] * power((LL)fac[b] * fac[a - b] % p, p - , p) % p;//三次乘法注意
n /= p;
m /= p;
}
return res;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &p);
init_fac(p);
printf("%d\n", lucas(n + m, m, p));
}
}

HDU 3037 Saving Beans(Lucas定理模板题)的更多相关文章

  1. hdu 3037 Saving Beans Lucas定理

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

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

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

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

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

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

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

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

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

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

  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

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

  9. hdu 3037 Saving Beans

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

随机推荐

  1. 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例)

    UBUNTU 14.04 LTS 为例 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例) 2013-05-01 16:02 2693人阅读 评论(0) 收藏 举报 情景描述: 计算机C1放 ...

  2. hive运行的相关配置

    一:执行SQL的方式 1.配置的键值 2.minimal下运行fetch 3.设定hive.fetch.task.conversion=more 4.在more下运行fetch 二:虚拟列 一共三个虚 ...

  3. C++ 简单 Hash容器的实现

    主要实现了以整数为关键字的hash,以key%m_nSize为哈希函数,以(hash(key)+i)%m_nSize重新寻址,并附带了elf_hash的实现,使用过程中可灵活修改. #ifndef _ ...

  4. html5优势

    1.首先,强化了Web网页的表现性能.除了可描绘二维图形外,还准备了用于播放视频和音频的标签.2.其次,追加了本地数据库等Web应用的功能.3.HTML5(text/html)浏览器将在错误语法的处理 ...

  5. ActiveMQ发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  6. css3 transition属性变化与animation动画的相似性以及不同点

    下面列子中的2个图片的效果. http://zqtest.e-horse.cn/DongXueImportedCar/assets/mouseOverAnimate.html 第一个为transiti ...

  7. Linux some command(continue...)

    挂载硬盘 sudo mount -t ext4 /dev/sdb1 /media/hadoop 自动挂载相关 sudo blkid sudo fdisk -l vim /etc/fstab cat / ...

  8. 【Algorithm】堆排,C++实现

    对一个数组中的元素按照顺序构建二叉树,就形成了一个(二叉)堆.(二叉树是虚拟的,并不是真的建立二叉树) 表示堆的数组A有两个重要属性:A.heapSize,表示堆里面有多少元素,数组里有多少元素在堆里 ...

  9. nginx支持cgi(c,c++)

    前段时间用我修改了tinyhttpd,让其cgi支持文件流,感觉满小巧,就应用上了.最近访问请求量上来而来,它对socket的各种异常状态处理不好,对于慢速的链接会占用我的线程.虽然我一直想仿出ten ...

  10. imx6 u-boot.bin 和 u-boot.imx

    有些MFG TOOL烧录工具使用了u-boot.imx,而不是原来的u-boot.bin文件进行烧录. 这两个镜像的区别是,u-boot.bin文件编译后,会在u-boot.bin的开头添加一个大小为 ...