Codeforces451E 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.
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 a single integer — the number of ways in which Devu can select the flowers modulo (109 + 7).
2 3
1 3
2
2 4
2 2
1
3 5
1 3 2
3
Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.
Sample 2. There is only one way of selecting 4 flowers: {2, 2}.
Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}.
【题目大意】
有n个花坛,要选s支花,每个花坛有f[i]支花,同一个花坛的花颜色相同,不同花坛的花颜色不同,问说可以有多少种组合。
【题解】
这题挺好的。
如果不考虑f[i]的限制,直接求,就是多重集合组合问题。
但是肯定有超限的地方,可能超限一个/两个。。
考虑容斥算
做多重集合插板时,我们令y = x + 1,变成方程Σyi = s + n的解的个数,y的取值为[1,+∞],插板时y至少取1
因此对于我们要求超限的xi,直接令yi = p + f[i] + 1,p的取值为[1,+∞],这样相当于不定方程右边减去f[i]+1,照常做没有上限的隔板
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <map> 9 #include <string> 10 #include <cmath> 11 #include <sstream> 12 #define min(a, b) ((a) < (b) ? (a) : (b)) 13 #define max(a, b) ((a) > (b) ? (a) : (b)) 14 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 15 template<class T> 16 inline void swap(T &a, T &b) 17 { 18 T tmp = a;a = b;b = tmp; 19 } 20 inline void read(long long &x) 21 { 22 x = 0;char ch = getchar(), c = ch; 23 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 24 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 25 if(c == '-') x = -x; 26 } 27 const long long INF = 0x3f3f3f3f; 28 const long long MAXN = 20 + 5; 29 const long long MOD = 1e9 + 7; 30 31 long long n,s,f[MAXN],pow2[MAXN],tot; 32 long long pow(long long a, long long b) 33 { 34 long long r = 1, base = a % MOD; 35 for(;b;b >>= 1) 36 { 37 if(b & 1) r *= base, r %= MOD; 38 base *= base, base %= MOD; 39 } 40 return r; 41 } 42 long long ni(long long x) 43 { 44 return pow(x, MOD - 2); 45 } 46 long long C(long long n, long long m) 47 { 48 if(n < 0 || m < 0 || n < m) return 0; 49 long long x = 1, y = 1; 50 if(m > n - m) m = n - m; 51 for(long long i = 1;i <= m;++ i) 52 x *= i, y *= (n - i + 1), x %= MOD, y %= MOD; 53 return y * ni(x) % MOD; 54 } 55 long long lucas(long long x, long long y) 56 { 57 if(x < y || x < 0 || y < 0) return 0; 58 if(y == 0) return 1; 59 return C(x % MOD, y % MOD) * lucas(x / MOD, y / MOD) % MOD; 60 } 61 int main() 62 { 63 64 pow2[0] = 1; 65 for(long long i = 1;i <= 20;++ i) pow2[i] = pow2[i - 1] << 1; 66 read(n), read(s); 67 for(long long i = 1;i <= n;++ i) read(f[i]); 68 long long ma = 1 << n, ans = 0; 69 for(long long S = 0;S < ma;++ S) 70 { 71 long long sum = 0, k = 0, tmp = S; 72 for(long long i = 1;i <= n;++ i) 73 if(S & pow2[i - 1]) 74 sum += f[i] + 1, ++ k; 75 long long tmp2 = lucas(s + n - sum - 1, n - 1); 76 if(k & 1) ans -= tmp2, ans %= MOD; 77 else ans += tmp2, ans = (ans % MOD + MOD) % MOD; 78 } 79 printf("%lld", ans); 80 return 0; 81 }