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;
}

Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥的更多相关文章

  1. Codeforces Round #258 (Div. 2)E - Devu and Flowers

    题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...

  2. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  3. Codeforces Round #428 (Div. 2) D. Winter is here 容斥

    D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...

  4. Codeforces Round #330 (Div. 2)B. Pasha and Phone 容斥

    B. Pasha and Phone   Pasha has recently bought a new phone jPager and started adding his friends' ph ...

  5. Codeforces Round #619 (Div. 2)C(构造,容斥)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; int main(){ ios::syn ...

  6. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  7. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

  8. Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

    Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...

  9. CF451E Devu and Flowers(容斥)

    CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...

随机推荐

  1. 一个ssm综合小案例-商品订单管理----写在前面

    学习了这么久,一直都是零零散散的,没有把知识串联起来综合运用一番 比如拦截器,全局异常处理,json 交互,RESTful 等,这些常见技术必须要掌握 接下来呢,我就打算通过这么一个综合案例把这段时间 ...

  2. Yii 自定义模型路径

    例如现有两个 Yii 项目,分别是 test1 和 test2.在 test1 中,已经有模型了,test2 直接调用 test1 中的模型,其实添加个别名,然后修改下配置即可. 先在 index.p ...

  3. 【转】CocoaLumberjack——带颜色的Log

    CHENYILONG Blog [转]CocoaLumberjack--带颜色的Log - 趣味苹果开发 - 博客园 转自:趣味苹果开发   CocoaLumberjack--带颜色的Log Coco ...

  4. 第5月第13天 node cnpm安装 babel

    1. https://nodejs.org/en/download/ http://www.runoob.com/react/react-install.html 2. npm install --s ...

  5. [译]How To Use the Linux Auditing System on CentOS 7

    本文是How To Use the Linux Auditing System on CentOS 7的中文版,翻译不到之处,还请指出和多多包涵.本文并不会完全遵从原文的一些格式,而是加入自己学习的理 ...

  6. 原生JS获取元素的位置与尺寸

    1.内高度.内宽度: 内边距 + 内容框 element.clientWidth element.clientHeight 2.外高度,外宽度: 边框 + 内边距 + 内容框 element.offs ...

  7. google浏览器测试时清理缓存、强制不用缓存刷新快捷键(常用、效率)

    Ctrl+Shift+Del  清除Google浏览器缓存的快捷键  Ctrl+Shift+R  重新加载当前网页而不使用缓存内容

  8. C. Ayoub and Lost Array(DP)

    (又是被队友带着上分的一场--) 题目链接:http://codeforces.com/contest/1105/problem/C 题目大意:给你n,l,r.每一个数都是在l,r范围之内,然后问你这 ...

  9. 日常训练赛 Problem C – Complete Naebbirac’s sequence

    比赛链接https://vjudge.net/contest/256988#status/17111202012/C/0/ 大意:三个操作,使得输入的数中,从1-n,每一个数出现的次数相同. wa代码 ...

  10. VirtualBox中CentOS遇到的问题

    centos7 安装步骤 https://www.cnblogs.com/hihtml5/p/8217062.html 静态ip设置 TYPE="Ethernet" PROXY_M ...