Devu and Flowers lucas定理+容斥原理

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 31 3

Output

2

Input

2 42 2

Output

1

Input

3 51 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}.

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define mod 1000000007
using namespace std;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans;
}
ll getc(ll a,ll b)
{
    if(a<b)return 0;
    if(b>a-b)b=a-b;
    ll s1=1,s2=1;
    for(ll i=0;i<b;i++)
    {
        s1=s1*(a-i)%mod;
        s2=s2*(i+1)%mod;
    }
    return s1*qpow(s2,mod-2)%mod;
}
ll lucas(ll n,ll k)
{
    if(k==0)return 1;
    return getc(n%mod,k%mod)*lucas(n/mod,k/mod)%mod;
}
int n;
ll s,f[25];
ll solve()
{
    ll ans=0;
    for(int i=0;i<(1<<n);i++)
    {
        ll sign=1,sum=s;
        for(int j=0;j<n;j++)
        {
            if(i&(1<<j))
            {
                sum-=f[j]+1;
                sign*=-1;
            }
        }
        if(sum<0)continue;
        ans+=sign*lucas(sum+n-1,n-1);
        ans%=mod;
    }
    return (ans+mod)%mod;
}
int main()
{
    cin>>n>>s;
    for(int i=0;i<n;i++)
        cin>>f[i];
    printf("%lld\n",solve());
    return 0;
}
时间: 2024-07-29 05:58:39

Devu and Flowers lucas定理+容斥原理的相关文章

cf451E Devu and Flowers 卢卡斯定理+容斥定理

题目:http://codeforces.com/problemset/problem/451/E 题意:有n个盒子(n<=20),每个盒子中有10^12个小球,现从每个盒子中取出若干球(可为0),求共取出s个小球(s<=10^14)的方案数. 组合数学问题,求C(n,m).但n,m过大时,可用卢卡斯定理. 卢卡斯定理:C(n,m) %p = C(n/p,m/p) * C(n%p,m%p) 从n个盒子中取出s个球的方案数,相当于插板,即 C(s+n-1,n-1).注意这是没有限制条件的情况.

codeforces #451E Devu and Flowers 不定方程解的个数+lucas定理

题意:链接 方法:不定方程解的个数+lucas定理 解析: 感觉做了这么多不定方程解的个数之后,每一次就是改一次组合数求法- -! 这次mod的是质数,并且观察到n,m可能大于mod,所以lucas裸上.. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 25 #define mod 1000000007 using nam

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

题目链接:Codeforces 451E Devu and Flowers 题目大意:有n个花坛.要选s支花,每一个花坛有f[i]支花.同一个花坛的花颜色同样,不同花坛的花颜色不同,问说能够有多少种组合. 解题思路:2n的状态,枚举说那些花坛的花取超过了,剩下的用C(n?1sum+n?1)隔板法计算个数.注意奇数的位置要用减的.偶数的位置用加的.容斥原理. #include <cstdio> #include <cstring> #include <cmath> #in

Codeforces Round #258 E Devu and Flowers --容斥原理

这题又是容斥原理,最近各种做容斥原理啊.当然,好像题解给的不是容斥原理的方法,而是用到Lucas定理好像.这里只讲容斥的做法. 题意:从n个容器中总共取s朵花出来,问有多少种情况.其中告诉你每个盒子中有多少朵花. 分析:其实就是求方程: x1+x2+...+xn = s 的整数解的个数,方程满足: 0<=x1<=a[1], 0<=x2<=a[2]... 设:A1 = {x1 >= a[1]+1} , A2 = {x2 >= a[2]+1} , .... , An = {

Codeforces Round #258 (Div. 2)Devu and Flowers 容斥原理

题目:Codeforces Round #258 (Div. 2)Devu and Flowers 题意:n个boxes ,第i个box有fi个flowers,每个boxes中的flowers完全相同,不同boxes的flowers不同,求从n个boxes中取出s个flowers的方案数.n<=20,s<=1e14,fi<=1e12. 排列组合的题目,一解法可用容斥原理(inclusion exclusion principle) . 有2中写法dfs和集合.下为集合写法. #inclu

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

题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的地,因此他每天上学时都只会向东或北行走:而小C又喜欢走不同的路径,因此他问你按照他走最短路径的规则,他可以选择的不同的上学路线有多少条.由于答案可能很大,所以小C只需要让你求出路径数mod P的值. 输入 第一行,四个整数N.M.T.P. 接下来的T行,每行两个整数,表示施工的路口的坐标. 输出 一

Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)

传送门 解题思路: 假如只有 s 束花束并且不考虑 f ,那么根据隔板法的可重复的情况时,这里的答案就是 假如说只有一个 f 受到限制,其不合法时一定是取了超过 f 的花束 那么根据组合数,我们仍然可以算出其不合法的解共有: 最后,由于根据容斥,减两遍的东西要加回来,那么含有偶数个 f 的项为正,奇数个时为负. 答案就是: 搜索答案,使用Lucas定理,计算组合数上下约去. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include&

Codeforces #258 Div.2 E Devu and Flowers

大致题意: 从n个盒子里面取出s多花,每个盒子里面的花都相同,并且每个盒子里面花的多数为f[i],求取法总数. 解题思路: 我们知道如果n个盒子里面花的数量无限,那么取法总数为:C(s+n-1, n-1) = C(s+n-1, s). 可以将问题抽象成:x1+x2+...+xn = s, 其中0<=xi <= f[i],求满足条件的解的个数. 两种方法可以解决这个问题: 方法一:这个问题的解可以等价于:mul = (1+x+x^2+...+x^f[1])*(1+x+x^2+...+x^f[2]

codeforces 451E Devu and Flowers

题意:有n个瓶子每个瓶子有 f[i] 支相同的颜色的花(不同瓶子颜色不同,相同瓶子花视为相同) 问要取出s支花有多少种不同方案. 思路: 如果每个瓶子的花有无穷多.那么这个问题可以转化为  s支花分到n个瓶子有多少种方案  用隔板法就能解决 C(s+n-1,n-1) .有限制之后我们可以 用 没限制的去减掉那些违反限制的 如果只有一个瓶子取得花超出上限 那么减去,两个瓶子 要加上(容斥原理) n只有20  就能暴力枚举那些取超过上限f[i]的瓶子并且在这些瓶子至少选出 f[i]+1 支花  统计