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;
    }
  • 相关阅读:
    Java读书笔记(2)-输入输出
    Java读书笔记(1)-异常处理
    Photoshop自动导出各尺寸Android和Iphone图标,支持新版Android Studio
    【原创】我的研究生活
    [原创]使用Fiddler抓取手机APP流量--360WIFI
    Federa 7 配置yum 源
    开源自己写的刷票器软件(windows和Android)
    更新linux kernel到3.14.10 LTS版后,virt-manager无法识别qemu hypervisor的问题
    Net Core Identity 身份验证:注册、登录和注销 (简单示例)
    Net Core的API文档工具Swagger
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5856732.html
Copyright © 2011-2022 走看看