zoukankan      html  css  js  c++  java
  • Codeforces 451E Devu and Flowers

    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).

    类似XDOJ卡尔的技能II,首先所有的情况是C(n+m-1,n)然后减去花坛中超过f[i]的,加上两个超过,减去三个的。。。

    #include <iostream>
    using namespace std;
    typedef long long LL;
    #define MOD 1000000007
    
    LL f[35],n,s;
    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 Fast_Mod(LL a,LL b,LL p)
    {
        LL res = 1,base = a;
      while (b){
        if (b & 1) res = (res * base) % p;
        base = base * base % p;
        b >>= 1;
      }
      return res;
    }
    
    LL C(LL n,LL m,LL p){
      if (n < m) return 0;
      if (m > n - m) m = n - m;
      LL s1 = 1,s2 = 1;
      for (LL i=0;i<m;i++){
        s1 = s1 * (n - i) % p;
        s2 = s2 * (i + 1) % p;
      }
    
      return s1 * Fast_Mod(s2,p-2,p) % p;
    }
    
    LL Lucas(LL n,LL m,LL p){
      if (m == 0) return 1;
      return C(n % p,m % p,p) * Lucas(n / p,m / p,p);
    }
    
    
    LL solve(){
      LL ans = 0;
    
      for (int i=0;i<(1<<n);i++){
        // cout << i << ans << endl;
        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, MOD);
        ans %= MOD;
      }
      return (ans + MOD) % MOD;
    }
    
    int main(){
        // freopen("test.in","r",stdin);
        n=read();s=read();
        for(int i=0;i<n;i++)
            f[i]=read();
        printf("%lld
    ", solve());
    }
    View Code
  • 相关阅读:
    Linux下解析域名命令-dig 命令使用详解
    重写、覆盖、重载、多态几个概念的区别分析
    介绍python中运算符优先级
    介绍Python中6个序列的内置类型
    Mysql(Mariadb)数据库主从复制
    winscp中使用sudo的方法
    git push跳过用户名和密码认证配置教程
    案例:通过shell脚本实现mysql数据备份与清理
    毕业季,我的Linux求职之路
    PHP和ajax详解
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7411172.html
Copyright © 2011-2022 走看看