1272: [BeiJingWc2008]Gate Of Babylon
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 239 Solved: 111
[ Submit][ Status][ Discuss]
Description
![](http://www.lydsy.com/JudgeOnline/images/1272/1.jpg)
Input
![](http://www.lydsy.com/JudgeOnline/images/1272/2.jpg)
Output
![](http://www.lydsy.com/JudgeOnline/images/1272/3.jpg)
Sample Input
![](http://www.lydsy.com/JudgeOnline/images/1272/4.jpg)
Sample Output
12
![](http://www.lydsy.com/JudgeOnline/images/1272/5.jpg)
HINT
Source
正面去做显然不可行。。。正难则反,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;
}