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. 复习hiernate

     Configuration Configuration 类负责管理 Hibernate 的配置信息 包括如下内容:1,Hibernate运行的底层信息:数据库的URL.用户名.密码.JDBC驱动类, ...

  2. [js高手之路]使用原型对象(prototype)需要注意的地方

    我们先来一个简单的构造函数+原型对象的小程序 function CreateObj( uName, uAge ) { this.userName = uName; this.userAge = uAg ...

  3. 【Js应用实例】jQuery监听回车键

    $(function(){ //焦点放在第一个文本输入框 $('input:text:first').focus(); //获取所有的输入框 var $inp=$('input'); //定义所有输入 ...

  4. IT科技企业逻辑思维面试题

    逻辑思维面试题 一.假设有一个池塘,里面有无穷多的水.现有2个空水壶,容积分别为5升和6升.问题是如何只用这2个水壶从池塘里取得3升的水.[请描述操作过程] 答:(1)先用容积为6升的水壶装满水: ( ...

  5. LAMP环境的搭建(三)----PHP7的安装

    centos自带的PHP源是5.4版本的.如果需要安装PHP 5.4 直接输入 yum install php -y 即可 因为PHP7性能巨大的提升,因此本文选择安装PHP7 如果已经安装过了php ...

  6. WeQuant交易策略—Dual Thrust

    Dual Thrust策略 策略介绍 Dual Thrust是一个趋势跟踪系统,由Michael Chalek在20世纪80年代开发,曾被Future Thruth杂志评为最赚钱的策略之一. Dual ...

  7. spring boot / cloud (十五) 分布式调度中心进阶

    spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...

  8. PHPcms9.6.0任意文件上传漏洞直接getshell 利用教程

    对于PHPcms9.6.0 最新版漏洞,具体利用步骤如下: 首先我们在本地搭建一个php环境,我这里是appserv或者使用phpnow (官网下载地址:http://servkit.org/) (只 ...

  9. Js原生轮播-直接上代码

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  10. jQuery遍历-同胞

    同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...