Codeforces Round 258 (Div. 2) E. Devu and Flowers 生成函数
题目链接
题目大意
有 n n n ( 1 ≤ n ≤ 20 ) (1\leq n \leq 20) (1≤n≤20) 个花瓶,第 i i i 个花瓶里有 f i f_i fi ( 1 ≤ f i ≤ 1 0 12 ) (1\leq f_i \leq 10^{12}) (1≤fi≤1012) 朵花。现在要选择 s s s ( 1 ≤ s ≤ 1 0 14 ) (1\leq s \leq 10^{14}) (1≤s≤1014)朵花。
求出有多少种方案。两种方案不同当且仅当两种方案中至少有一个花瓶选择花的数量不同,答案对 1 0 9 + 7 10^9+7 109+7 取模。
思路
取第 i i i 种花的生成函数可以表示为 : f i ( x ) = f_i(x)= fi(x)= 1 + x + x 2 + x 3 + ⋅ ⋅ ⋅ x f i 1+x+x^2+x^3+ \cdot \cdot \cdot x^{f_i} 1+x+x2+x3+⋅⋅⋅xfi.
则选取方案的生成函数可以表示为 : G ( x ) = G(x)= G(x)= f 1 ( x ) ⋅ f 2 ( x ) ⋅ ⋅ ⋅ f n ( x ) = f_1(x) \cdot f_2(x) \cdot \cdot \cdot f_n(x) = f1(x)⋅f2(x)⋅⋅⋅fn(x)= ( 1 − x f 1 + 1 ) ( 1 − x f 2 + 1 ) ( 1 − x f 3 + 1 ) ⋅ ⋅ ⋅ ( 1 − x f n + 1 ) ( 1 − x ) n \frac {(1-x^{f_1+1})(1-x^{f_2+1})(1-x^{f_3+1}) \cdot \cdot \cdot (1-x^{f_n+1})} {(1-x)^n} (1−x)n(1−xf1+1)(1−xf2+1)(1−xf3+1)⋅⋅⋅(1−xfn+1) = = = ∏ i = 1 n ( 1 − x f i + 1 ) ( 1 − x ) n \frac {\prod_{i=1}^{n}(1-x^{f_i+1})}{(1-x)^{n}} (1−x)n∏i=1n(1−xfi+1) = = = ∏ i = 1 n ( 1 − x f i + 1 ) \prod_{i=1}^{n}(1-x^{f_i+1}) ∏i=1n(1−xfi+1) ⋅ \cdot ⋅ ∑ i = 0 ∞ \sum_{i=0}^\infty ∑i=0∞ ( i + n − 1 i ) {i+n-1\choose i} (ii+n−1) x i x_i xi = = = ∏ i = 1 n ( 1 − x f i + 1 ) \prod_{i=1}^{n}(1-x^{f_i+1}) ∏i=1n(1−xfi+1) ⋅ \cdot ⋅ ∑ i = 0 ∞ \sum_{i=0}^\infty ∑i=0∞ ( i + n − 1 n − 1 ) {i+n-1\choose n-1} (n−1i+n−1) x i x_i xi .
则 [ x s ] G ( x ) [x^s]G(x) [xs]G(x)即为所求的答案。
令 A ( x ) = ∏ i = 1 n ( 1 − x f i + 1 ) A(x)=\prod_{i=1}^{n}(1-x^{f_i+1}) A(x)=∏i=1n(1−xfi+1), B ( x ) = ∑ i = 0 ∞ B(x)=\sum_{i=0}^\infty B(x)=∑i=0∞ ( i + n − 1 n − 1 ) x i {i+n-1\choose n-1} x_i (n−1i+n−1)xi.
由于 n ≤ 20 n\leq20 n≤20 , 所以 A ( x ) A(x) A(x) 最多只有 2 20 2^{20} 220 项,可以直接枚举,即 a n s = [ x s ] G ( x ) = ∑ i = 0 2 n [ x i ] A ( x ) ⋅ [ x s − i ] B ( x ) = ans=[x^s]G(x)=\sum_{i=0}^{2^n}[x^i]A(x) \cdot [x^{s-i}]B(x)= ans=[xs]G(x)=∑i=02n[xi]A(x)⋅[xs−i]B(x)= ∑ i = 0 2 n [ x i ] A ( x ) ⋅ ( s − i + n − 1 n − 1 ) \sum_{i=0}^{2^n}[x^i]A(x) \cdot {s-i+n-1\choose n-1} ∑i=02n[xi]A(x)⋅(n−1s−i+n−1),由于 n n n 很小可以暴力计算组合数,总的时间复杂度为 O ( n ⋅ 2 n ) O(n \cdot 2^n) O(n⋅2n).
code
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pii pair<int, int>
using namespace std;
const int mod = 1e9 + 7;
int f[30];
int ksm(int x, int k)
{
int res = 1;
while (k > 0)
{
if (k & 1)
res = res * x % mod;
x = x * x % mod;
k >>= 1;
}
return res;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, s;
cin >> n >> s;
int fac = 1;
for (int i = 1; i <= n; ++i)
{
cin >> f[i - 1];
if (i < n)
fac = fac * i % mod;
}
int infac = ksm(fac, mod - 2);
int ans = 0;
for (int i = 0; i < (1ll << n); ++i)
{
int cnt = 0, sum = 0;
for (int j = 0; j < n; ++j)
{
if ((1ll << j) & i)
cnt++, sum += f[j] + 1;
}
if (sum > s)
continue;
int tmp = 1;
for (int j = 0; j < n - 1; ++j)
{
int p = (s - sum + n - 1 - j) % mod;
tmp = tmp * p % mod;
}
tmp = tmp * infac % mod;
ans = (ans + ((cnt & 1) ? mod - tmp : tmp) % mod) % mod;
}
cout << (ans + mod) % mod << '\n';
return 0;
}