Saving Beans

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2079    Accepted Submission(s): 748

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.

 
 
 
2 3 107 ==>10 ==>C(5,2)
 
{ 0,0
   0,1  1,0
   1,1, 2,0  0,2
   3,0  0,3   1,2  2,1
}
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
typedef __int64 LL; LL dp[]; void init(LL p){
int i;
dp[]=;
for(i=;i<=p;i++)
dp[i]=(dp[i-]*i)%p;
}
LL pow_mod(LL a,LL n,LL p)
{
LL ans=;
while(n)
{
if(n&) ans=(ans*a)%p;
n=n>>;
a=(a*a)%p;
}
return ans;
}
LL C(LL a,LL b,LL p)
{
if(a<b) return ;
if(b>a-b) b=a-b;
LL sum1=dp[a];
LL sum2=(dp[b]*dp[a-b])%p;
sum1=(sum1*pow_mod(sum2,p-,p));
return sum1;
}
LL Lucas(LL n,LL m,LL p)
{
LL ans=;
while(n&&m&&ans)
{
ans=(ans*C(n%p,m%p,p))%p;
n=n/p;
m=m/p;
}
return ans;
}
int main()
{
int T;
LL n,m,p;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&n,&m,&p);
init(p);
if(n>m)swap(n,m);
LL ans= Lucas(n+m,m,p);
printf("%I64d\n",ans);
}
return ;
}

hdu 3037 Saving Beans的更多相关文章

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

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

  2. hdu 3037 Saving Beans Lucas定理

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

  3. hdu 3037——Saving Beans

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

  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 (Lucas法则)

    主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...

  6. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

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

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

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

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

  9. HDU 3073 Saving Beans

    Saving Beans Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

随机推荐

  1. 进击的java - tomcat的安装,配置都正确之后,还是报错

    1.问题 配置Apatch Tomcat过程报错: The CATALINA_HOME environment variable is not defined correctly.This envir ...

  2. Myeclipse10编写jsp时出现 Multiple annotations found at this line:

    今天,老师讲完课做了一个小练习,就是编写一个jsp页面.写完后,我发现少些了点东西,我准备使用<% %>添加是发现,报错了 Multiple annotations found at th ...

  3. BeanFactory

            Spring容器,最基本的接口就是BeanFactory 负责创建,配置,管理bean 它有一个子接口ApplicationContext并将功能扩展.         理论上bean ...

  4. 大数据量的csv文件如何导入到 sql 数据库

    BULK INSERT dbo.T_test001 FROM 'E:\bus_20160316\bus全量评级及借款编号_20160316.csv' WITH ( FIELDTERMINATOR =' ...

  5. 远程mysql服务器无法连接解决方案

    错误现象:Habon被拒绝. 远程服务器无法连接从两个方面看 1.是否能ping通远程服务器 windows下查看防火墙是否已关闭 linux下查看iptables等 2.数据库是否有开用户管理权限 ...

  6. Linux centOS7 下安装mysql5.7.10

    1:下载二进制安装包 http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz 2:解压到 ...

  7. Another app is currently holding the yum lock; waiting for it to exit...另一个应用程序在占用yum lock,等待其退出。

    这种情况下通常是由于yum的时候意外退出造成的,虽然也给出提示当前占用进行的id,但是执行kill -9 3599 强制杀死进程后,情况依然没能改变. 备注:(-9是强制杀死) 主要原因是因为,yum ...

  8. zw版_Halcon图像库delphi接口文件

    zw版_Halcon图像库delphi接口文件 Halcon图像库delphi接口文件,根据安装时用户设置的文件目录不同,会有所差异,笔者一般安装在delphi的import目录下.     参见:& ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON AddNoiseWhite

    zw版[转发·台湾nvp系列Delphi例程]HALCON AddNoiseWhite unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...

  10. split function of Perl,Python,Awk

    使用中常用到Perl,Python,AWK,R, 虽然Java,C,C++,Vala也学过但是就是不喜欢,你说怎么办. 看来一辈子脚本的命. Perl @rray = split /PATTERN/, ...