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

Total Submission(s): 2610    Accepted Submission(s): 966

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
 

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
ll qpow(ll a,ll b,ll c)
{
ll ans=1;
while(b>0)
{
if(b&1)
ans=ans*a%c;
a=a*a%c;
b>>=1;
}
return ans;
}
ll fac[100000];
ll fmt(ll a,ll b,ll c)
{
return fac[a]*qpow(fac[b]*fac[a-b]%c,c-2,c)%c;
}
ll lucas(ll a,ll b,ll c)
{
ll ans=1;
while(a>0&&b>0)
{
if(a%c<b%c)
return 0;
ans=ans*fmt(a%c,b%c,c)%c;
a/=c;
b/=c;
}
return ans;
}
void create(ll a)
{
fac[0]=1;
for(ll i=1;i<a;i++)
fac[i]=fac[i-1]*i%a;
}
int main()
{
int T;
cin>>T;
while(T--)
{
ll n,m,p;
cin>>n>>m>>p;
create(p);
cout<<lucas(n+m,m,p)<<endl;
}
}

hdu3037Saving Beans的更多相关文章

  1. HDU3037Saving Beans(组合数+lucas定理)

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

  2. hdu3037Saving Beans——卢卡斯定理

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037 卢卡斯定理模板——大组合数的取模 代码如下: #include<iostream> #i ...

  3. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  4. beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userHandler': Injection of resource dependencies failed;

    一个错误会浪费好多青春绳命 鉴于此,为了不让大家也走弯路,分享解决方案. [错误代码提示] StandardWrapper.Throwableorg.springframework.beans.fac ...

  6. 利用beans.xml进行简单的Spring应用上下文创建与使用

    继上次配置Spring完成后,我们来创建一个简单的例程来理解Spring中利用beans.xml创建应用上下文的方法. 程序路径包为:com.spring.kinghts(kinght单词拼写错误,怕 ...

  7. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  8. [转]spring beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. XML中<beans>中属性概述

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

随机推荐

  1. 关于http接口开发中json格式数据编码问题处理

    关于http接口开发中json格式数据编码问题处理 在实际工作中,接口很多时候返回json格式,但有时返回的格式会有编码问题 假设如下接口:http://service.test.com/interf ...

  2. Linux php/php-fpm 安装,配置

    1> 下载php源代码   #wget  http://cn2.php.net/get/php-5.4.30.tar.gz/from/this/mirror   官网5.2的版本号貌似没提供   ...

  3. win7问题解决,凭据管理器和无法访问,不允许一个用户使用一个以上用户名与服务器或共享资源进行多重连接。

    WIN7凭据管理器,如果你用一个帐号远程登录以后在电脑中会记住这个信息,假如你想用另外的帐号,那么就到控制面板-凭据管理器里中进行修改或者删除. 如果你登录以后提示,“无法访问.不允许一个用户使用一个 ...

  4. android细节之禁用activity的系统的默认切换效果

    网上有非常多方法来禁用系统的默认效果,这里贴上来我觉得最简单的方法. overridePendingTransition(Animation.INFINITE, Animation.INFINITE) ...

  5. 每日算法之三十三:Trapping Rain Water

    这是一个非常有意思的问题,求解最大容积问题,值得动脑筋想一想. 原题例如以下: Given n non-negative integers representing an elevation map ...

  6. poj2761(treap入门)

    给n个数,然后m个询问,询问任意区间的第k小的数,特别的,任意两个区间不存在包含关系, 也就是说,将所有的询问按L排序之后, 对于i<j ,   Li < Lj 且 Ri < Rj ...

  7. Hadoop学习笔记(两)设置单节点集群

    本文描写叙述怎样设置一个单一节点的 Hadoop 安装.以便您能够高速运行简单的操作,使用 Hadoop MapReduce 和 Hadoop 分布式文件系统 (HDFS). 參考官方文档:Hadoo ...

  8. unity3d 数学的数学基础和辅助类

    转载注明smartdot:http://my.oschina.net/u/243648/blog/67193 1.  数学(点乘/叉乘)/unity3d的数学辅助类 2.  坐标系统(本地/世界/屏幕 ...

  9. C++ Primer高速学习 第一章 获得二:输入和输出 (IO)

    什么是输入输出.即Input-Output,缩写是非常装B的IO?请看经典民间解释: C++语言的输入输出是指信息从外部输入设备(如键盘.磁盘等)向计算机内部(内存)输入(即Input)和从内存向外单 ...

  10. JDK基本介绍

    JDK这是Java Development Kit 缩写,中国被称为Java开发套件.由SUN该公司提供.这是Java应用程序开发提供了编译和执行环境,所有的Java写程序都依赖于它. JDK能够将J ...