hdu3037 Saving Beans(个数可为0的特殊“插板法”+推公式+组合数取摸(需预处理素数阶乘,否则TLE))

171 篇文章 0 订阅
79 篇文章 0 订阅


Link:http://acm.hdu.edu.cn/showproblem.php?pid=3037

Saving Beans

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


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
 


题意:求在n棵树上摘不超过m颗豆子的方案总数,结果对p取模。

编程思想:由于每棵树上都可摘0~m颗豆子(存在分组的元素个数为0的情况),则对于特定的m颗豆子,利用插板法分成m+1份,第m+1份表示原先要摘的m颗豆子减去已经在n颗树采摘豆子的总数后剩下的数目,所以有C(n+m-1,m)种方案,所以总数为

C(n-1,0)+C(n,1)+...+C(n+m-1,m)

= C(n,0)+C(n,1)+C(n+1,2)+...+C(n+m-1,m)

= C(n+m,m)

答案就是C(n+m,m) % p,其中p是素数。利用Lucas定理对组合数求模,同时观察到素数模较小(100000),需要先对其进行素数阶乘处理,否则会TLE!!!


AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#define LL long long
#define MAXN 1000010
using namespace std;
const  int N=20;//模方程数 
LL a[N],mod[N];
int cnt;//素数个数
int prim[2];//存储素数
/*const int maxx=10005;//最大素数上限(适用于要模的素数最大不超过10000的情况下) 
int frc[3300][maxx];//frc[i][j]表示(j!)%(第i个素数prim[i])
int id[maxx];//素数编号 
bool vis[maxx];

void init()//素数筛选及打表求:(j!)%(第i个素数prim[i]) 
{
	for(int i=2;i<maxx;i++) if(!vis[i]){
		prim[cnt]=i;
		id[i]=cnt;
		for(int j=i*2;j<maxx;j+=i)vis[j]=1;
		frc[cnt][0]=1;
		for(int j=1;j<prim[cnt];j++) frc[cnt][j]=frc[cnt][j-1]*j%prim[cnt];
		cnt++;
	}
} */
int frc[100011];
void Single_init(int p)//只初始化一个素数的阶乘
{
	frc[0]=1;
	for(int j=1;j<=p;j++) 
		frc[j]=(LL)frc[j-1]*j%p;
} 
/*LL mul(LL a,LL b,LL mod)//a*b%mod
{
	LL ans=0;
	while(b){
		if(b&1)
			ans=(ans+a)%mod;
		b>>=1;
		a=(a+a)%mod;
	}
	return ans;
}*/

LL quick_mod(LL a,LL b,LL m)//a^b%m 
{
	LL ans=1;
	a%=m;
	while(b)
	{
		if(b&1)
		{
			ans=ans*a%m;
		}			
		b>>=1;
		a=a*a%m;
	}
	return ans;
}

LL getC(LL n,LL m,int cur)//C(n,m)%mod[cur]
{
	LL p=mod[cur];//p为要取的素数模mod[cur] 
	if(m>n)
		return 0;
	if(m>n-m)
		m=n-m;
	LL ans=1;
/*	for(int i=1;i<=m;i++)
	{
		LL a=(n+i-m)%p;
		LL b=i%p;
		//ans=mul(ans,mul(a,quick_mod(b,p-2,p),p),p);//p为素数,i对p的逆元可以不用扩张欧几里得进行求解  re=i^(P-2) 
		ans = ans * (a * quick_mod(b, p-2,p) % p) % p;  
	}*/
	//注释掉这个,然后用以下方法求解提高速率 
	//直接用打表方式求解(适用于要模的素数最大不超过10000的情况下)
//	int num=id[p];//素数编号 
	ans=(LL)frc[n]*quick_mod((LL)frc[n-m]*frc[m]%p,p-2,p)%p;
	return ans;
}

LL Lucas(LL n,LL k,int cur)//求C(n,k)%mod[cur] 
{
	LL p=mod[cur];
	if(k==0)
		return 1%p;
	//return getC(n%p,k%p,cur)*Lucas(n/p,k/p,cur)%p;
	return getC(n % p, k % p,cur) * Lucas(n / p, k / p,cur) % p;  
}

/*void extend_Euclid(LL a,LL b,LL &x,LL &y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return;
	}
	extend_Euclid(b,a%b,x,y);
	LL tmp=x;
	x=y;
	y=tmp-a/b*y;
}

LL CRT(LL a[],LL m[],int k)//求C(n,m)%M,其中M=(m0*m2*…*m(k-1)),mi为素数,则先用a[i]存储模方程C(n,m)%mi,
{                           //m[]存储所有素数因子mi,k表示总共有k个模方程,返回C(n,m)%M的值 
	LL M=1;
	LL ans=0;
	for(int i=0;i<k;i++)
		M*=mod[i];
	for(int i=0;i<k;i++)
	{
		LL x,y,tmp;
		LL Mi=M/m[i];
		extend_Euclid(Mi,m[i],x,y);
		if(x<0)
		{
			x=-x;
			tmp=mul(Mi,x,M);
			tmp=mul(tmp,a[i],M);
			tmp=-tmp;
		}
		else
		{
			tmp=mul(Mi,x,M);
			tmp=mul(tmp,a[i],M);
		}
		ans=(ans+tmp)%M;
	}
	while(ans<0)
		ans+=M;
	return ans;
}*/
LL ans;
int main()
{
	//freopen("D:\\in.txt","r",stdin);
	int T,n,m,p;
	T=0;
	//init();
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&p);
	//	T++;
		Single_init(p);//先处理单个素数的阶乘,否则TLE!!! 
		mod[0]=p;
		ans=Lucas(n+m,m,0);
		printf("%I64d\n",ans);
	 } 
  	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值