hdu3037Saving Beans

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

2009 Multi-University Training Contest 13 - Host
by HIT

#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;
	}
}
时间: 2024-10-11 14:57:09

hdu3037Saving Beans的相关文章

HDU3037Saving Beans(组合数+lucas定理)

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 supp

Spring之 Auto-Wiring All Beans of Compatible Type

Auto-Wiring All Beans of Compatible Type @Autowired 注解按类型(type)依赖入住的时候,可以把类型兼容的所有类注入到数组.链表.map等集合数据结构中.如:mybatis中TypeHandler为例: package com.doctor.practice01; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.Resul

XML中&lt;beans&gt;属性

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring

springsecurity启动出现org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You must use a 3.0 schema with Spring Security 3.0.

在换了spring-security的jar包以后启动出现org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You must use a 3.0 schema with Spring Security 3.0.Please update your schema declarations to the 3.0.3 schema (spring-securi

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named &#39;App&#39; is defined

工具:Eclipse mars 环境:jdk1.8 说明:这是在学习Spring Task时遇到的一个bug,代码如下: 定时任务类: package com.task.test; import java.util.Date; import org.springframework.stereotype.Component; @Component public class App { public void execute1(){ System.out.printf("Task: %s, Curr

spring beans源码解读

spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.annotation, 支持包,提供对java 5注解处理bean样式的支持.org.springframework.beans.factory, 实现spring轻量级IoC容器的核心包.org.springframework.beans.factory.access, 定位和获取bean工程的辅助工具类

异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundExcepti

hdu2845——Beans

Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3011    Accepted Submission(s): 1450 Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled w

关于命名空间beans错误

命名空间自动生成xmlns:beans="http://www.springframework.org/schema/beans"  测试正常 加载tomcat会报出   cvc-elt.1: 找不到元素 'beans' 的声明    的异常 修改成      xmlns="http://www.springframework.org/schema/beans"