Codeforces Round #258 (Div. 2)E(组合数+容斥原理)

E. Devu and Flowers
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 200 ≤ 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 test(s)
input
2 3
1 3
output
2
input
2 4
2 2
output
1
input
3 5
1 3 2
output
3

题意:给出n种花,以及每种花的数量 a[i] ,然后要求从中选出s束花,问有多少种选法

思路:首先我们先不考虑每种花会超出数量的情况,那么一共是C(s+n-1,n-1)种选法,现在假设在一些方案里面第i种花选的超出了所给的最大数量那么就要减去C(s+n-1-

         a[i]-1,n-1),同时这样减可能会减多一些可能的方案,那么还要加回来,可以采用容斥原理,有偶数种花超出了就+,奇数就减,因为n最大只有20,可以状态压缩,1表

         示超出,0表示未超出,暴力计算完就好了~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;
#define mod 1000000007
typedef long long ll;
ll a[22];
ll s;
int n;

ll pow_ni(ll n,ll x)
{
    ll ans=1;
    ll temp=x;
    int j=0;

    while(n)
    {
        if((1<
       
       
         =n-k+1;i--)x=(x*(i%mod))%mod; for(i=k;i>=1;i--)y=y*i%mod; return x*pow_ni(mod-2,y)%mod; } int main() { while(cin>>n>>s) { int i,j; int ss; ll sum=0; for(i=0;i 
        
          >a[i]; sum+=a[i]; } if(s==0)cout<<1< 
          
         
       
      
      
     
     
    
    
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值