Codeforces Round #258 E Devu and Flowers --容斥原理

这题又是容斥原理,最近各种做容斥原理啊。当然,好像题解给的不是容斥原理的方法,而是用到Lucas定理好像。这里只讲容斥的做法。

题意:从n个容器中总共取s朵花出来,问有多少种情况。其中告诉你每个盒子中有多少朵花。

分析:其实就是求方程: x1+x2+...+xn = s 的整数解的个数,方程满足: 0<=x1<=a[1], 0<=x2<=a[2]...

设:A1 = {x1 >= a[1]+1} , A2 = {x2 >= a[2]+1} , .... , An = {xn >= a[n]+1}. 全集S = (n+s-1,s)

所以容斥原理可求得答案。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define lll __int64
#define ll long long
using namespace std;
#define N 100007 ll a[];
ll inv[]; ll fastm(ll a,ll b)
{
ll res = 1LL;
while(b)
{
if(b&1LL)
res = (res*a)%Mod;
b >>= ;
a = (a*a)%Mod;
}
return res;
} ll comb(ll n,ll r)
{
if(r > n)
return ;
r = min(r,n-r);
n%=Mod;
ll ans = 1LL;
for(int i=;i<=r-;i++)
{
ans = ans*(n-i)%Mod;
ans = ans*inv[i+]%Mod;
}
return ans;
} int calc(int s)
{
int cnt = ;
while(s)
{
if(s&)
cnt++;
s >>= ;
}
return cnt;
} int main()
{
int n,i;
ll s;
for(i=;i<=;i++)
inv[i] = fastm(i,Mod-);
while(cin>>n>>s)
{
for(i=;i<n;i++)
cin>>a[i];
ll ans = ;
int S = (<<n)-;
for(int state=;state<=S;state++)
{
ll r = s;
int tmp = state;
int cnt = calc(state);
i=;
while(i<n)
{
if(tmp&)
r -= (a[i]+);
tmp >>= ;
i++;
}
if(r < )
continue;
ll cb = comb(n+r-,r);
if(cnt%)
cb = -cb;
ans = (ans+cb+Mod)%Mod;
}
printf("%I64d\n",ans);
}
return ;
}
上一篇:leetcode 402. Remove K Digits 、321. Create Maximum Number


下一篇:Google protobuf的安装及使用