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. js基础语法习题补充

    输入三个整数,x,y,z,最终以从小到大的方式输出. <<script> var x=prompt("请输入数字") var y=prompt("请输入 ...

  2. 简单倒计时js代码

    //倒计时 var timer=null; var interval = 1000; function ShowCountDown(year,month,day,hour,minute,second, ...

  3. 如何设计和实现一个web app

    web app简介 web app其实不算是什么新鲜的东西,相比于传统的web和传统的app,web app这种web和app相结合的产物有的优点如下: 1. 开发上web app更有便捷性,ios开 ...

  4. dotnet使用Selenium执行自动化任务

    如果要做百度文库,百度贴吧,百度知道签到,你,会怎么做?前不久我还会觉得这好像太麻烦了,now,soeasy. 自动化测试工具:Selenium Selenium是一个用于Web应用程序测试的工具.S ...

  5. $MarkDown$ 中使用$ \LaTeX$ 数学式

    最近看了些机器学习的书籍, 想写点笔记记录下. 由于需要使用到很多的数学推导, 所以就看了下如何在 Markdown 中插入数学式,发现在 Markdown 中可以直接插入 LaTeX 数学式. 排版 ...

  6. 灾难恢复-boot分区的恢复方法

    boot分区是系统启动中最重要的部分,如果服务器由于病毒攻击又或者被管理员误删除了boot分区.那么就会存在潜在的风险.为什么说是潜在的风险?因为boot分区被删除后系统仍在继续运行,看似无状况但是在 ...

  7. [2017-07-18]logstash配置示例

    提醒 /etc/logstash/conf.d/下虽然可以有多个conf文件,但是Logstash执行时,实际上只有一个pipeline,它会将/etc/logstash/conf.d/下的所有con ...

  8. SpringMVC + Mybatis bug调试 SQL正确,查数据库却返回NULL

    今天碰到个bug,有点意思 背景是SpringMVC + Mybatis的一个项目,mapper文件里写了一条sql 大概相当于 select a from tableA where b = &quo ...

  9. 增强遍历和Object多参数遍历

    public class T2 { public void t1(Object o){//Object是任何类型,多态 System.out.println(o.toString()); } publ ...

  10. 利用wireshark任意获取qq好友IP实施精准定位

    没事玩一把,感觉还挺有趣,首先打开wireshark: 不管你连接的什么网,如图我连接的是WLAN,双击进入如图界面: ctrl-f进行搜索:如图 选择分组详情,字符串,并输入020048.这时候你就 ...