Codeforces Round #258 (Div. 2)Devu and Flowers 容斥原理
2014-07-26 21:17
519 查看
题目:Codeforces Round #258 (Div. 2)Devu and Flowers
题意:n个boxes ,第i个box有fi个flowers,每个boxes中的flowers完全相同,不同boxes的flowers不同,求从n个boxes中取出s个flowers的方案数。n<=20,s<=1e14,fi<=1e12.
排列组合的题目,一解法可用容斥原理(inclusion exclusion principle) 。
有2中写法dfs和集合。下为集合写法。
题意:n个boxes ,第i个box有fi个flowers,每个boxes中的flowers完全相同,不同boxes的flowers不同,求从n个boxes中取出s个flowers的方案数。n<=20,s<=1e14,fi<=1e12.
排列组合的题目,一解法可用容斥原理(inclusion exclusion principle) 。
有2中写法dfs和集合。下为集合写法。
#include <bits\stdc++.h> 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; }
相关文章推荐
- Codeforces Round #258 E Devu and Flowers --容斥原理
- Codeforces#258 (Div.2) E - Devu and Flowers 容斥
- Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
- Codeforces Round #330 (Div. 2) (595A,595B(容斥原理))
- Codeforces Round #258 (Div. 2) B. Sort the Array (模拟)
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game
- Codeforces Round #330 (Div. 2) B. Pasha and Phone(容斥原理)
- Codeforces Round #249 (Div. 2)C Devu and Partitioning of the Array
- Codeforces Round #258 (Div. 2)题解(ALL)
- Codeforces Round #251 (Div. 2) D. Devu and his Brother
- Codeforces Round #249 (Div. 2)C Devu and Partitioning of the Array
- Codeforces Round #258 (Div. 2)
- Codeforces Round #258 (Div. 2)E(组合数+容斥原理)
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array
- Codeforces Round#251(Div 2)D Devu and his Brother
- Codeforces Round #258 (Div. 2)
- Codeforces Round #258 (Div. 2) A. Game With Sticks(数学题)
- Codeforces Round #258 (Div. 2) A. Game With Sticks
- Codeforces Round #258 (Div. 2)[ABCD]