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

Input
2 3
1 3
Output
2
Input
2 4
2 2
Output
1
Input
3 5
1 3 2
Output
3

题意:给定N种花,每种花有Fi朵,现在要取M朵花,问有多少种方案。

思路:母函数或者容斥定理,当然,学过数学竞赛的应该知道结论,怎么用容斥定理去做。我更倾向于用母函数去做,虽然方程是一样的,但是感觉后者好理解一些。

母函数 :    (1+X^1...+X^f1)*(1+X^1...+Xf2)*...(1+X^1...+X^fn)

=(1-X^(f1+1))*(1-X^(f2+1))...(1-X^(fn+1)) / ((1-X)^N)

=(1-X^(f1+1))*(1-X^(f2+1))...(1-X^(fn+1)) * ((1+X^1+X^2+...)^N)。

对于前面部分((1-X^(f1+1))*(1-X^(f2+1))...(1-X^(fn+1)) )可以枚举O(2^N),然后剩余部分,就是组合问题了,假设前面的和为S,那么就要在后面部分(((1+X^1+X^2+...)^N))拿M-S个,然后根据组合公式,C(M-S+N-1,N-1)。

组合公式那里利用Lucas可以降低复杂度,但是我还是觉得复杂度过不去,O(N * 2^N *Mod *LogMod )可能是数据水了,然后只花了900ms。

容斥定理:假设没有限制F,则得到ans=C(M+N-1,N-1)。但是有限制后有的会超过Fi,枚举超过的那几个,blabla,大概是这个方向啦,具体的我也没有细究。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int Mod=1e9+;
int N; ll M,f[],ans;
ll qpow(ll a,ll x)
{
ll res=; a%=Mod;
while(x){
if(x&1LL) res=res*a%Mod;
a=a*a%Mod;
x>>=;
} return res;
}
ll Com(ll a,ll b)
{
ll res=,fm=,fz=;
if(a-b<b) b=a-b;
for(int i=;i<=b;i++){
fm=fm*i%Mod;
fz=fz*(a-i+)%Mod;
}
res=fz*qpow(fm,Mod-)%Mod;
return res;
}
ll Lucas(ll a,ll b)
{
if(b==) return ;
return Lucas(a/Mod,b/Mod)*Com(a%Mod,b%Mod)%Mod;
}
void solve()
{
for(int i=;i<(<<N);i++){
ll sig=,sum=M;
for(int j=;j<N;j++){
if((<<j)&i) sum-=(f[j]+),sig*=-;
}
if(sum<) continue;
ans=(ans+sig*Lucas(N+sum-,N-))%Mod;
}
}
int main()
{
scanf("%d%lld",&N,&M);
for(int i=;i<N;i++)
scanf("%lld",&f[i]);//,sum+=f[i];
solve();
printf("%lld\n",(ans%Mod+Mod)%Mod);
return ;
}

CodeForces-451E:Devu and Flowers (母函数+组合数+Lucas定理)的更多相关文章

  1. Codeforces 451E Devu and Flowers【容斥原理+卢卡斯定理】

    题意:每个箱子里有\( f[i] \)种颜色相同的花,现在要取出\( s \)朵花,问一共有多少种颜色组合 首先枚举\( 2^n \)种不满足条件的情况,对于一个不被满足的盒子,我们至少拿出\( f[ ...

  2. Codeforces 451E Devu and Flowers(容斥原理)

    题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组 ...

  3. codeforces 451E. Devu and Flowers 容斥原理+lucas

    题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...

  4. CodeForces - 451E Devu and Flowers (容斥+卢卡斯)

    题意:有N个盒子,每个盒子里有fi 朵花,求从这N个盒子中取s朵花的方案数.两种方法不同当且仅当两种方案里至少有一个盒子取出的花的数目不同. 分析:对 有k个盒子取出的数目超过了其中的花朵数,那么此时 ...

  5. Codeforces 451E Devu and Flowers(组合计数)

    题目地址 在WFU(不是大学简称)第二次比赛中做到了这道题.高中阶段参加过数竞的同学手算这样的题简直不能更轻松,只是套一个容斥原理公式就可以.而其实这个过程放到编程语言中来实现也没有那么的复杂,不过为 ...

  6. codeforces 451E Devu and Flowers

    题意:有n个瓶子每个瓶子有 f[i] 支相同的颜色的花(不同瓶子颜色不同,相同瓶子花视为相同) 问要取出s支花有多少种不同方案. 思路: 如果每个瓶子的花有无穷多.那么这个问题可以转化为  s支花分到 ...

  7. uoj86 mx的组合数 (lucas定理+数位dp+原根与指标+NTT)

    uoj86 mx的组合数 (lucas定理+数位dp+原根与指标+NTT) uoj 题目描述自己看去吧( 题解时间 首先看到 $ p $ 这么小还是质数,第一时间想到 $ lucas $ 定理. 注意 ...

  8. 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理

    4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 95  Solved: 33[Submit][Statu ...

  9. [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)

    题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

随机推荐

  1. [Go]字典(map)的操作和约束

    字典(map)存储的是键值对(key-value pair,一个键值对代表了一对键和值.一个键和一个值分别代表了一个从属于某一类型的独立值,把它们两个捆绑在一起就是键值对,也称“键-元素对”)的集合 ...

  2. 在mac下安装matplotlib,xlrd

    brew install freetype brew install libpng sudo easy_install pip#图形显示模块 sudo pip install matplotlib 输 ...

  3. POJ 2478 欧拉函数打表的运用

    http://poj.org/problem?id=2478 此题只是用简单的欧拉函数求每一个数的互质数的值会超时,因为要求很多数据的欧拉函数值,所以选用欧拉函数打表法. PS:因为最后得到的结果会很 ...

  4. BZOJ 1303: [CQOI2009]中位数图 【水题】

    给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Out ...

  5. Codeforces225E - Unsolvable

    Portal Description 求所有对于方程\[z=\left \lfloor \frac{x}{2} \right \rfloor+y+xy\]不存在正整数解\((x,y)\)的\(z\)中 ...

  6. HDU 4436 (后缀自动机)

    HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...

  7. PatentTips - Solid State Memory Wear Leveling

    BACKGROUND OF THE INVENTION Solid-state memory devices encompass rewritable non-volatile memory devi ...

  8. python执行

    转载:https://www.cnblogs.com/zflibra/p/4180796.html

  9. 是男人就下100层【第四层】——Crazy贪吃蛇(2)

    在上一篇<是男人就下100层[第四层]--Crazy贪吃蛇(1)>中我们让贪吃蛇移动了起来,接下来我们来实现让贪吃蛇能够绕着手机屏幕边线移动而且能够改变方向 一.加入状态并改动代码 首先我 ...

  10. Android中个人推崇的数据库使用方式

    手机应用开发中常常会使用到数据库存储一些资料或者进行数据缓存,android中为我们提供了一个轻量的数据库.在上层进行了一层封装,同一时候还为我们提供了ContentProvider的框架.方便我们进 ...