Home > Web Front-end > HTML Tutorial > Codeforces Round #258 (Div. 2)Devu and Flowers Inclusion and Exclusion Principle_html/css_WEB-ITnose

Codeforces Round #258 (Div. 2)Devu and Flowers Inclusion and Exclusion Principle_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:01:02
Original
1156 people have browsed it

Title: Codeforces Round #258 (Div. 2) Devu and Flowers Question meaning: n boxes, the i-th box has fi flowers, the flowers in each box are exactly the same, and the flowers in different boxes are different, find the following The number of options for removing s flowers from n boxes. n<=20, s<=1e14, fi<=1e12. For permutation and combination questions, the inclusion exclusion principle can be used to solve the problem. There are 2 ways to write dfs and collection. The following is how to write a collection.

#include using namespace std;const int MOD = 1e9 + 7;typedef long long LL;LL invv[25];LL inv(LL x)/// 求逆元{    return x == 1 ? 1LL : (MOD - MOD / x) * inv (MOD % x) % MOD;}LL Cmn(LL n, LL m) ///求组合数{    LL ret = 1;    for (int i = 1; i <= m; i++)        ret = (n - i + 1) % MOD * ret % MOD * invv[i] % MOD;    return ret;}int calc(int x){    int ret = 0;    while (x)    {//        x -= x & (-x);        x=x&(x-1);        ret ^= 1;    }    return ret;}int n;LL s, a[25];int main(){    for (int i = 1; i < 25; i++) invv[i] = inv(i);    cin >> n >> s;    for (int i = 0; i < n; i++)        cin >> a[i];    int ALL = (1 << n) - 1;    LL ans = 0;    for (int i = 0; i <= ALL; i++)///遍历所有集合    {        LL nows = s;        int num = 0;///统计当前集合元素个数,以确定符号        for (int j = 0; j < n; j++)        {            if (i & (1 << j))            {                nows -= a[j] + 1;                num++;            }        }        if (nows < 0) continue;        if (num & 1)///集合含有奇数个元素,符号为-            ans -= Cmn(nows + n - 1, n - 1);        else///集合含有偶数个元素,符号为+            ans += Cmn(nows + n - 1, n - 1);        ans %= MOD;    }    cout << (ans + MOD) % MOD << endl;    return 0;} 


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template