Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

E. Devu and Flowers

题目连接:

http://codeforces.com/contest/451/problem/E

Description

Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.

Now Devu wants to select exactly s flowers from the boxes to decorate his garden. Devu would like to know, in how many different ways can he select the flowers from each box? Since this number may be very large, he asks you to find the number modulo (109 + 7).

Devu considers two ways different if there is at least one box from which different number of flowers are selected in these two ways.

Input

The first line of input contains two space-separated integers n and s (1 ≤ n ≤ 20, 0 ≤ s ≤ 1014).

The second line contains n space-separated integers f1, f2, ... fn (0 ≤ fi ≤ 1012).

Output

Output a single integer — the number of ways in which Devu can select the flowers modulo (109 + 7).

Sample Input

2 3
1 3

Sample Output

2

Hint

题意

有n个盒子,然后每个盒子有f[i]个,你需要拿出来s个球,问你一共有多少种选择。

题解:

题目改一下,改成你有s个球,要放进n个盒子,问一共有多少种方案。

隔板法去放就好了。

再容斥处理那个f[i]

代码

#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define LL long long
using namespace std;
LL qmod(LL a,LL b)
{
    LL res=1;
    if(a>=MOD)a%=MOD;
    while(b)
    {
        if(b&1)res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
LL invmod[50];
LL C(LL n,LL m)
{
    if(n<m)return 0;
    LL ans=1;
    for(int i=1;i<=m;++i)
        ans=(n-i+1)%MOD*ans%MOD*invmod[i]%MOD;
    return ans;
}
LL f[30],n,s;
LL ans;
void gao(int now,LL sum,int flag)
{
    if(sum>s)return ;
    if(now==n)
    {
        ans+=flag*C(s-sum+n-1,n-1);
        ans%=MOD;
        return ;
    }
    gao(now+1,sum,flag);
    gao(now+1,sum+f[now]+1,-flag);
}
int main() {
    for(int i=1;i<=20;++i)
        invmod[i]=qmod(i,MOD-2);
    cin>>n>>s;
    for(int i=0;i<n;++i)
        cin>>f[i];
    ans=0;
    gao(0,0,1);
    cout<<(ans%MOD+MOD)%MOD<<endl;
    return 0;
}
posted @ 2016-09-09 15:12  qscqesze  阅读(395)  评论(0编辑  收藏  举报