zoukankan      html  css  js  c++  java
  • Codeforces 451E

    Devu and Flowers

    如果不考虑限制答案为 C(s + n - 1, n - 1), 即把s个球分到n个箱子中,箱子可以为空的方案数。

    壮压枚举几个超过了, 容斥一下。

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    using namespace std;
    
    const int N = 1e6 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    
    int n;
    LL s, f[20], ans;
    
    LL fastPow(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 calc(LL n, LL m) {
        if(n < 0 || n < m) return 0;
        if(n == m || !m) return 1;
        LL a = n % mod, b = 1;
        for(int i = 1; i < m; i++)
            a = a * ((n - i) % mod) % mod, b = b * (i + 1) % mod;
        return a * fastPow(b, mod - 2) % mod;
    }
    
    int main() {
        cin >> n >> s;
        for(int i = 0; i < n; i++) cin >> f[i];
        for(int S = 0; S < (1 << n); S++) {
            LL ret = 0, op = 1;
            for(int i = 0; i < n; i++)
                if(S >> i & 1) ret += f[i] + 1, op = -op;
            ans = (ans + op * calc(s - ret + n - 1, n - 1) + mod) % mod;
        }
        cout << ans << "
    ";
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    P1983 车站分级
    P1807 最长路
    P1347 排序
    P1073 最优贸易 (tarjan缩点+dp)
    最小费用最大流解决KM匹配问题
    CF191C Fools and Roads
    case when
    防呆机制
    DbCommand :执行超时已过期。完成操作之前已超时或服务器未响应。
    存储过程带参数和sqlcommand
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10359321.html
Copyright © 2011-2022 走看看