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;
}
Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥的更多相关文章
- Codeforces Round #258 (Div. 2)E - Devu and Flowers
题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #619 (Div. 2)C(构造,容斥)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; int main(){ ios::syn ...
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
- 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\) ...
- CF451E Devu and Flowers(容斥)
CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...
随机推荐
- Codeforces 238 div2 B. Domino Effect
题目链接:http://codeforces.com/contest/405/problem/B 解题报告:一排n个的多米诺骨牌,规定,若从一边推的话多米诺骨牌会一直倒,但是如果从两个方向同时往中间推 ...
- 第13月第25天 ios11 uitableview reloaddata contentsize
1. [tableView reloadData]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_ ...
- 第7月第19天 swift on linux
1. https://github.com/iachievedit/moreswift http://dev.iachieved.it/iachievedit/more-swift-on-linux/ ...
- 使用 scm-manager 搭建 git/svn 代码管理仓库(一)
1.在官网上下载scm-manager 下载地址 https://www.scm-manager.org/download/ 选择下载文件 2. 配置java 环境 参照文章:https://jin ...
- 善用backtrace解决大问题【转】
转自:https://www.2cto.com/kf/201107/97270.html 一.用途: 主要用于程序异常退出时寻找错误原因 二.功能: 回溯堆栈,简单的说就是可以列出当前函数调用关系 三 ...
- php扩展Redis功能
php扩展Redis功能 1 首先,查看所用php编译版本V6/V9 在phpinfo()中查看 2 下载扩展 地址:https://github.com/nicolasff/phpredis/dow ...
- thymeleaf:访问静态方法
<p class="left tel" th:if="${#strings.startsWith(T(net.common.util.tool.common.Req ...
- react-native 报错
报错信息: java.lang.RuntimeException: Unable to load script from assets 'index.android.bundle'. Make sur ...
- Coursera台大机器学习技法课程笔记14-Radial Basis Function Network
将Radial Basis Function与Network相结合.实际上衡量两个点的相似性:距离越近,值越大. 将神经元换为与距离有关的函数,就是RBF Network: 可以用kernel和RBF ...
- 有没有 linux 命令可以获取我的公网 ip, 类似 ip138.com 上获取的 ip?
curl ipinfo.iocurl ifconfig.me 阿里云 :139.129.242.131赤峰: 219.159.38.197开平: 221.194.113.146定州: 121 ...