Codeforces Round #258 (Div. 2)

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 ≤ 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 test(s)
Input
2 3
1 3
Output
2
Input
2 4
2 2
Output
1
Input
3 5
1 3 2
Output
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}.

题意:有N种花,每种最多f[i]枝,从中选s枝花,问有多少种选法。

题解:隔板法+容斥原理+Lucas定理算大组合数+求逆元

如此难的题,我不懂!我是看 http://hzwer.com/3810.html 学会的。

首先看没有f[i]枝数限制的话,可以用隔板法,N种花选s枝,相当于s个相同的球放到N个箱子有多少种放法。隔板法要求每个箱子至少放1个球,所以我们先增加N个假球来搞隔板法(相当于隔完板把每个盒子去掉一个球,就能算到有空的的情况了),N+s个球有N+s-1个空隙,分N个箱子需要N-1个隔板,种类数有C(N+s-1 , N-1)种。

然后观察有f[i]限制的情况。我们可以假装取超了,假装已经在第i个盒子取了f[i]+1个球,把总球数减去(f[i]+1),用这个总球数可以用C(N'+s-1,N'-1)算出取超了的情况的种类数。

然后可能有0个盒子取超、1个盒子盒子取超、2个盒子取超……等等好多情况,这些情况还有互相重复的,这就要用到容斥原理。

ans=0个超的情况数 - 各种1个超的情况数 +各种2个超的情况数 - 各种3个超的情况数……

知道要算什么了,接下来看怎么算。

N<=20,s<=10^14,各种盒子取超的情况可以2^20枚举。算C(N+s-1 - ...    , N-1)就比较难,不可能直接算。

用到Lucas定理:C(n,m)%p=C(n/p,m/p)*C(n%p,m%p),左边继续递归Lucas,右边用逆元来算。

怎么用逆元:C(n,m)不是先算出分子和分母,然后分子除以分母嘛,我们可以当做用分子乘以分母的逆元。分母的逆元可以用超碉的一个定理:

费马小定理a^(p-1)=1(mod p),a为质数

a^(p-2)=a^(-1)(mod p),那么a^(p-2)就是a在modp意义下的逆元。

我们就用快速幂求出分母^(p-2),就是逆元了。

用这么多知识才能解E题,我都怕,是时候变碉了。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout) const ll maxs=1e14;
const ll MOD=1e9+;
ll a[];
ll n,s,ans; ll PowerMod(ll a, ll b) {
ll tmp = a, ret = ;
while (b) {
if (b & ) ret = ret * tmp % MOD;
tmp = tmp * tmp % MOD;
b >>= ;
}
return ret;
} ll calC(ll n,ll m){
m=n-m>m?m:n-m;
ll up=,down=;
int i;
for(i=;i<=m;i++){
down*=i;
down%=MOD;
up*=(n-i+);
up%=MOD;
}
return (up*PowerMod(down,MOD-))%MOD;
} ll Lucas(ll n, ll m) {
if(m==)return ;
return (Lucas(n/MOD, m/MOD)*calC(n%MOD, m%MOD))%MOD;
} void attack(ll now,ll sum,ll flag){
if(sum<n)return;
if(now==n){
//printf("%I64d C(%I64d,%I64d)=",flag,sum-1 , n-1);
//printf("%I64d\n",Lucas(sum-1,n-1));
ans+=flag*Lucas(sum- , n-);
ans%=MOD;
return;
}
attack(now+,sum,flag);
attack(now+,sum-a[now]-,-flag);
} int main() {
int i;
scanf("%I64d%I64d",&n,&s);
REP(i,n) scanf("%I64d",&a[i]);
ans=;
attack(,n+s,);
printf("%I64d\n",((ans%MOD)+MOD)%MOD);
return ;
}

CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)的更多相关文章

  1. CodeForces-451E:Devu and Flowers (母函数+组合数+Lucas定理)

    Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...

  2. CF451E Devu and Flowers 解题报告

    CF451E Devu and Flowers 题意: \(Devu\)有\(N\)个盒子,第\(i\)个盒子中有\(c_i\)枝花.同一个盒子内的花颜色相同,不同盒子的花颜色不同.\(Devu\)要 ...

  3. CF451E Devu and Flowers(容斥)

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

  4. bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)

    Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...

  5. BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers

    Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...

  6. hdu6397 Character Encoding 隔板法+容斥原理+线性逆元方程

    题目传送门 题意:给出n,m,k,用m个0到n-1的数字凑出k,问方案数,mod一个值. 题目思路: 首先如果去掉数字范围的限制,那么就是隔板法,先复习一下隔板法. ①k个相同的小球放入m个不同的盒子 ...

  7. CF451E Devu and Flowers 数论

    正解:容斥+Lucas定理+组合数学 解题报告: 传送门! 先mk个我不会的母函数的做法,,, 首先这个题的母函数是不难想到的,,,就$\left (  1+x_{1}^{1}+x_{1}^{2}+. ...

  8. 【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理

    题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的 ...

  9. HDU3037 Saving Beans(Lucas定理+乘法逆元)

    题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...

随机推荐

  1. [转]django自定义表单提交

    原文网址:http://www.cnblogs.com/retop/p/4677148.html 注:本人使用的Django1.8.3版本进行测试 除了使用Django内置表单,有时往往我们需要自定义 ...

  2. MVVM中数据验证之 ViewModel vs. Model

                                                      MMVM模式示意图. View绑定到ViewModel,然后执行一些命令在向它请求一个动作.而反过来 ...

  3. Microsoft-Office-Professional-Plus-2007

    Microsoft-Office-Professional-Plus-2007password:(也有自带的)DP37G-8BBDM-9Y4BW-WT2K8-2WRMJ P64QH-V3F2K-RXY ...

  4. 洛谷P1462 通往奥格瑞玛的道路[二分答案 spfa 离散化]

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  5. 3357: [Usaco2004]等差数列

    3357: [Usaco2004]等差数列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 321  Solved: 153[Submit][Statu ...

  6. VS一直停留在“正在还原nuget程序包”

    VS一直停留在“正在还原nuget程序包” 在开发何问起收藏夹的时候,准备在WinFrom中加入网页浏览器,于是下载了一个CEFSharp的源码,生成解决方案的时候,一直提示“正在还原nuget程序包 ...

  7. ReactNative新手学习之路06滚动更新ListView数据的小示例

    本节带领大家学习使用ListView 做一个常用的滚动更新数据示例: 知识点: initialListSize={200} 第一次加载多少数据行 onEndReached={this.onEndRea ...

  8. 【ASP.NET实战教程】基于ASP.NET技术下多用户博客系统全程实战开发(NNblog)

    岁末主推:牛牛老师主讲,多用户博客系统,基于ASP.NET技术,年后将带来移动业务平台项目项目目标: 打造个性品牌Blogo,定制多用户博客 为每一个博客用户提供个性化的 blogo解决方案,打造精品 ...

  9. win7 cmd 操作mysql数据库

    一 ,对MySql服务器的开启,重启,关闭等操作       当然,可以在win7的界面环境下,关闭或开启MySql服务.但是经常找不到win7的服务管理器,主要定位方法有二:命令行下输入servic ...

  10. rsyslog配置报错解决

    配置过程中,查看/var/log/meassage 有报错信息: action '*' treated as ':omusrmsg:*' - please use ':omusrmsg:*' synt ...