Codeforces 451 E Devu and Flowers
Discription
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).
Example
2 3
1 3
2
2 4
2 2
1
3 5
1 3 2
3
Note
Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.
Sample 2. There is only one way of selecting 4 flowers: {2, 2}.
Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}.
容斥一波直接带走
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1100005;
const int ha=1000000007;
int ci[33],tag[maxn],n,inv[33];
ll sum[maxn],S,ans=0; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline void init(){
ci[0]=1;
for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
inv[1]=1;
for(int i=2;i<=20;i++) inv[i]=-inv[ha%i]*(ll)(ha/i)%ha+ha;
} inline int C(int x,int y){
int an=1;
for(int i=1;i<=y;i++) an=an*(ll)(x-i+1)%ha*(ll)inv[i]%ha;
return an;
} inline void solve(){
tag[0]=1,sum[0]=0;
for(int i=0;i<ci[n];i++){
if(i) tag[i]=-tag[i^(i&-i)];
sum[i]=sum[i^(i&-i)]+sum[i&-i];
if(sum[i]<=S) ans=add(ans,add(tag[i]*C((S-sum[i]+n-1)%ha,n-1),ha));
}
} int main(){
init();
scanf("%d%I64d",&n,&S);
for(int i=0;i<n;i++) scanf("%I64d",sum+ci[i]),sum[ci[i]]++;
solve();
printf("%I64d\n",ans);
return 0;
}
Codeforces 451 E Devu and Flowers的更多相关文章
- Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)
传送门 解题思路: 假如只有 s 束花束并且不考虑 f ,那么根据隔板法的可重复的情况时,这里的答案就是 假如说只有一个 f 受到限制,其不合法时一定是取了超过 f 的花束 那么根据组合数,我们仍然可 ...
- 【Codeforces 258E】 Devu and Flowers
[题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...
- Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥
E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...
- Codeforces 451E Devu and Flowers(容斥原理)
题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- E. Devu and Flowers
E. Devu and Flowers time limit per test 4 seconds memory limit per test 256 megabytes input standard ...
- CF451E Devu and Flowers 解题报告
CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...
- CF451E Devu and Flowers(容斥)
CF451E Devu and Flowers(容斥) 题目大意 \(n\)种花每种\(f_i\)个,求选出\(s\)朵花的方案.不一定每种花都要选到. \(n\le 20\) 解法 利用可重组合的公 ...
- BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers
Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...
随机推荐
- 歌乐第二弹:C++九九八十一
第一弹传送门:极乐净土 二话不说,上代码(注意事项在第一弹里): #include <windows.h> //q前缀为低音,g为高音,s前缀为半音阶 const int q1 = 131 ...
- Mac屏幕亮度保存
关于保存屏幕亮度的方法,论坛上已有几种,搜索 NVRAM 会出来很多教程,在此不再详述,可以参考帖子http://www.idelta.info/archives/nvram_on_hackintos ...
- GIMP语言设置
初学GIMP,需要设置语言:点击 编辑 - 首选项 其他的配置如: 配置快捷键 自己熟悉吧!
- C语言之extern、const、volatile
extern: extern修饰变量,声明该变量为外部文件的全局变量.若使用外部全局变量,必须用extern声明. extern修饰函数,声明该函数为外部函数.extern修饰的函数形参必须与原函数一 ...
- Python面向对象(类之间的关系)(三)
类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻 ...
- python爬取博客圆首页文章链接+标题
新人一枚,初来乍到,请多关照 来到博客园,不知道写点啥,那就去瞄一瞄大家都在干什么好了. 使用python 爬取博客园首页文章链接和标题. 首先当然是环境了,爬虫在window10系统下,python ...
- PAT Basic 1022
1022 D进制的A+B 输入两个非负10进制整数A和B(<=2^30^-1),输出A+B的D (1 < D <= 10)进制数. 输入格式: 输入在一行中依次给出3个整数A.B和D ...
- JSTL 配置
pache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载地址:http:// ...
- Linux下配置MySQL主从复制
一.环境准备 本次准备两台Linux主机,操作系统都为CentOS6.8, 都安装了相同版本的MySQL.(MySQL5.7). 主从服务器的防火墙都开启了3306端口. 相关信息如下: [主服务器] ...
- vm下-kali-linux-xfce-2018.1的简略安装
1.选择版本为 debian 系统,因为是kali是基于debian的linux发行版 2.在安装首页选择 Graphical install,图形化安装,之后会让选择语言,选择简体中文,中国等,这些 ...