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. Python之创建tuple和“可变”的tuple

    Python之创建tuple tuple是另一种有序的列表,中文翻译为" 元组 ".tuple 和 list 非常类似,但是,tuple一旦创建完毕,就不能修改了. 同样是表示班里 ...

  2. 如何在html中插入视频

    如何在html中插入视频 1,插入优酷视频: 在优酷分享界面有个html代码,直接复制放入body中,定义div的align居中即可 2.插入本地视频:用video属性  用mp4格式 <vid ...

  3. To do

    小事{ android values public.xml 树.图的所有遍历方式和优劣 } 大事{ 通读android所有官网文档. android多dex多res开发框架. java AOT(and ...

  4. SpringMVC+MyBatis(最新)

    目前主流的Web MVC框架,除了Struts这个主力 外,还有Spring MVC,主要是由于Spring MVC配置比较简单,使用起来也十分明了,非常灵活,与Spring 集成较好,对RESTfu ...

  5. 雾里看花终隔一层——探析package和import

    package是什么 package好比java用来组织文件的一种虚拟文件系统.package把源代码.java文件,.class文件和其他文件有条理的进行一个组织,以供java来使用. 源代码的要求 ...

  6. JQuery:JQuery的尺寸

    JQuery:尺寸 介绍:通过 jQuery,很容易处理元素和浏览器窗口的尺寸.jQuery 提供多个处理尺寸的重要方法:width().height().innerHeight().outerWid ...

  7. [BS-07] 创建和使用PCH File

    创建和使用PCH File 1.创建PCH File File - iOS Other - PCH File - PrefixHeader.pch 写法如下: #ifndef PrefixHeader ...

  8. python_操作oracle数据库

    1. cx_Oracle Python 连接Oracle 数据库,需要使用cx_Oracle 包. 该包的下载地址:http://cx-Oracle.sourceforge.net/ 下载的时候,注意 ...

  9. 点云匹配和ICP算法概述

    Iterative Closest Point (ICP) [1][2][3] is an algorithm employed to minimize the difference between ...

  10. 一个前端html模板处理引擎(javascript) - pure

    做后台开发(java/python)的同学开发web应用,对于前端页面生成技术并不陌生,像jsp,freemark等.开发UGC类型的互联网站,因为要SEO友好,所以一般都会在后台用模板引擎直接生成好 ...