zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    JS的中数的取值范围的大小
    前端通过xlsx插件导入excel
    H5和安卓原生进行交互的操作流程记录
    javascript中字符串和数字之间互相转换的方法总结
    gitlab代码合并到主分支
    typeof和valueof、instance of 之间的区别
    javascript中map会改变原始的数组吗
    使用typescript来写react遇到的一些问题
    使用javascript进行时间数据格式的转换
    在vue的移动端项目中使用vue-echarts
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5856732.html
Copyright © 2011-2022 走看看