1272: [BeiJingWc2008]Gate Of Babylon

1272: [BeiJingWc2008]Gate Of Babylon

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 239   Solved: 111
[ Submit][ Status][ Discuss]

Description

Input

Output

Sample Input

Sample Output

12

HINT

Source

[ Submit][ Status][ Discuss]



正面去做显然不可行。。。正难则反,ans = 所有方案 - 不合法方案

定义所有方案为每一种宝具可以随便用,不限次数

但是只需要选择不超过M件就行了,还要求和,有点麻烦?

再加一种宝具,还是能随便用,但是任何一种使用了k个该宝具的方案,对应的真实方案是选了m - k个宝具

这样统计就简单了,隔板法,共C(n+m,n)种

怎么统计不合法的?可以发现,任何一种不合法方案,肯定存在至少一种超级神器i,使得i的使用次数ki大于bi

那么这些神奇就先拿出bi + 1个吧,剩下的随意分配,按照之前的方法统计方案,反正已经不合法了

注意到t很小,只有15,可以枚举出所有这样的组合,用容斥原理解决


组合数的话Lucas定理就行了。。。要注意的是,Lucas定理中的组合数C(n,m)还是按照原计算法则计算

一开始居然写if (n <= m) return 1;。。搞不懂自己在想什么

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 1E5 + 10;
typedef long long LL;

int n,t,m,Ans,b[20],fac[maxn],inv[maxn];
LL p;

int Mul(const LL &x,const LL &y) {return x*y%p;}
int Add(const LL &x,const LL &y) {return (x + y) % p;}
int Dec(const LL &x,const LL &y) {return (x - y + p) % p;}

int C(const int &N,const int &M)
{
	if (N < M) return 0;
	int Inv = Mul(inv[M],inv[N-M]);
	return Mul(Inv,fac[N]);
}

int Lucas(const int &N,const int &M)
{
	if (!M) return 1;
	return Mul(C(N%p,M%p),Lucas(N/p,M/p));
}

int ksm(int x,int y)
{
	int ret = 1;
	for (; y; y >>= 1)
	{
		if (y&1) ret = Mul(ret,x);
		x = Mul(x,x);
	}
	return ret;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> t >> m >> p; fac[0] = 1;
	for (int i = 1; i < p; i++) fac[i] = Mul(fac[i-1],i);
	inv[p-1] = ksm(fac[p-1],p - 2);
	for (int i = p - 2; i >= 0; i--) inv[i] = Mul(inv[i+1],i+1);
	for (int i = 0; i < t; i++) scanf("%d",&b[i]);
	for (int i = 0; i < (1<<t); i++)
	{
		int tot = 0,res = m;
		for (int j = 0; j < t; j++)
			if (i&(1<<j))
			{
				res -= (b[j] + 1); ++tot;
				if (res < 0) break;
			}
		if (res < 0) continue;
		int ret = Lucas(res + n,n);
		if (tot&1) Ans = Dec(Ans,ret);
		else Ans = Add(Ans,ret);
	}
	cout << Ans;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值