Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

Hint

Hint

For 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
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3033 3038 3036 3035 3034 
 
 
Tips:
  答案是求C(n+m,m)% P
  这里P不大,N过大,用lucas定理可以求出来;
 
Code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; long long t,n,m,ans,p,f[],x,y; void exgcd(long long a,long long b){
if(b==){
x=; y=;
return;
}
exgcd(b,a%b);
long long t=x; x=y; y=t-(a/b)*y;
return;
} long long lucas(long long a,long long b,long long MOD){
long long res=;
while(a&&b){
long long aa=a%MOD,bb=b%MOD;
if(aa<bb) return ;
res=res*f[aa]%MOD;
exgcd(f[aa-bb]*f[bb],MOD);
x=(x%MOD+MOD)%MOD;
res=(res*x)%MOD;
a=a/MOD;
b=b/MOD;
}
return res;
} void init(long long MOD){
f[]=;
for(int i=;i<=MOD;i++){
f[i]=f[i-]*i%MOD;
}
} int main(){
scanf("%lld",&t);
for(int i=;i<=t;i++){
scanf("%lld%lld%lld",&n,&m,&p);
n=n+m;
init(p);
ans=lucas(n,m,p);
printf("%lld\n",ans);
}
}

hdu3037 Saving Beans的更多相关文章

  1. hdu3037 Saving Beans(Lucas定理)

    hdu3037 Saving Beans 题意:n个不同的盒子,每个盒子里放一些球(可不放),总球数<=m,求方案数. $1<=n,m<=1e9,1<p<1e5,p∈pr ...

  2. HDU3037 Saving Beans(Lucas定理+乘法逆元)

    题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...

  3. [HDU3037]Saving Beans,插板法+lucas定理

    [基本解题思路] 将n个相同的元素排成一行,n个元素之间出现了(n-1)个空档,现在我们用(m-1)个“档板”插入(n-1)个空档中,就把n个元素隔成有序的m份,每个组依次按组序号分到对应位置的几个元 ...

  4. hdu 3037 Saving Beans Lucas定理

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

  5. hdu 3037 Saving Beans

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

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

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

  7. HDOJ 3037 Saving Beans

    如果您有n+1树,文章n+1埋不足一棵树m种子,法国隔C[n+m][m] 大量的组合,以取mod使用Lucas定理: Lucas(n,m,p) = C[n%p][m%p] × Lucas(n/p,m/ ...

  8. hdu 3037——Saving Beans

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

  9. poj—— 3037 Saving Beans

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

随机推荐

  1. 再起航,我的学习笔记之JavaScript设计模式12(适配器模式)

    适配器模式 适配器模式(Adapter): 将一个类(对象)的接口(方法或属性)转化成为另外一个接口,使类(对象)之间接口的不兼容问题通过适配器得以解决. 适配相似的框架 不知道大家有没有遇到过这种场 ...

  2. Redis入门 (CentOS7 + Redis-3.2.1)

    1. 编译安装1.1 下载redis # cd /tmp/# wget http://download.redis.io/releases/redis-3.2.1.tar.gz# tar zxvf r ...

  3. 【2016北京集训测试赛(八)】 crash的数列 (思考题)

    Description 题解 题目说这是一个具有神奇特性的数列!这句话是非常有用的因为我们发现,如果套着这个数列的定义再从原数列引出一个新数列,它居然还是一样的...... 于是我们就想到了能不能用多 ...

  4. 你所不知道的 CSS 动画技巧与细节

    怕标题起的有点大,下述技巧如果你已经掌握了看看就好,欢迎斧正,本文希望通过介绍一些 CSS 不太常用的技巧,辅以一些实践,让读者可以更加深入的理解掌握 CSS 动画. 废话少说,直接进入正题,本文提到 ...

  5. Sublime Text 2安装图解

    Sublime Text 2安装图解.. ---------------- ---------------- ---------------- ---------------- ----------- ...

  6. WeQuant交易策略—BOLL

    BOLL(布林线指标)策略 简介 BOLL(布林线)指标是技术分析的常用工具之一,由美国股市分析家约翰•布林根据统计学中的标准差原理设计出来的一种非常简单实用的技术分析指标.一般而言,价格的运动总是围 ...

  7. Django 学习笔记(三)模板导入

    本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...

  8. jQuery绑定事物处理器

    绑定与移除1..bind() 绑定事件可以有2个或者3个参数:第一个参数为事件类型 第二个参数为处理函数 第三个为布尔类型 on()事件代替2..delegate() 事件委托,三个参数,第一个为选择 ...

  9. SSH:dataSource配置问题

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  10. PHP访问连接MYSQL数据库

    1.连接数据库       使用mysql_connect()函数建立与MySQL数据库的连接 源码:$con=mysql_connect("主机名或IP","用户名&q ...