LuoGu: CF995F Cowmpany Cowmpensation
CF: F. Cowmpany Cowmpensation
考虑求出恰好使用了 $i$ 个值的方案数 $g _ i$ ,那么有答案 $\sum _ {i = 1} ^ n \binom m i g _ i$ ,其中 $\binom m i$ 可以 $O(n)$ 暴力算。
注意到虽然 $m$ 很大,但是实际上用到的不超过 $n$ 个,不妨令用 $n$ 个数,那么考虑 DP ,$f _ {i, j}$ 表示在 $i$ 点,选择了数 $j$ 的方案数。方程 $f _ {u, i} = \prod _ v \sum _ {k = 1} ^ i f _ {v, k}$ 。除了最大数 $i$ 以外的 $i - 1$ 个数中,若恰好使用了 $k$ 个数的种类数为 $g _ k$ ,那么在 $f _ {1, i}$ 中贡献了 $\binom { i - 1 } { k - 1} $ 次,那么可以发现 $\sum _ {k = 1} ^ i \binom {i - 1} {i - k} g _ k = f _ {1, i}$ 。容斥找到最后的答案。
查看代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <cstdio> #include <vector> #define pb push_back using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; const int N = 3e3 + 10, mod = 1e9 + 7; vector <int> e[N]; int n, m, f[N][N], g[N]; int inv[N], fact[N], ifact[N]; int C (int a, int b) { return a < b ? 0 : (LL)fact[a] * ifact[b] % mod * ifact[a - b] % mod; } void init () { inv[1] = 1; for (int i = 2; i < N; ++i) inv[i] = -(LL)(mod / i) * inv[mod % i] % mod; fact[0] = ifact[0] = 1; for (int i = 1; i < N; ++i) { fact[i] = (LL)fact[i - 1] * i % mod; ifact[i] = (LL)ifact[i - 1] * inv[i] % mod; } } void dfs (int u) { for (int i = 1; i <= n; ++i) f[u][i] = 1; for (int v : e[u]) { dfs(v); for (int k = 1; k <= n; ++k) f[u][k] = (LL)f[u][k] * f[v][k] % mod; } for (int i = 2; i <= n; ++i) f[u][i] = (f[u][i] + f[u][i - 1]) % mod; } int main () { init (); read(n, m); for (int i = 2, a; i <= n; ++i) read(a), e[a].pb(i); dfs(1); for (int i = 1; i <= n; ++i) { g[i] = (f[1][i] - f[1][i - 1]) % mod; for (int j = 1; j < i; ++j) g[i] = (g[i] - (LL)C(i - 1, i - j) * g[j]) % mod; } int res = 0; for (int i = 1; i <= min(n, m); ++i) { int t = ifact[i]; for (int j = 0; j < i; ++j) t = (LL)t * (m - j) % mod; res = (res + (LL)t * g[i]) % mod; } write((res + mod) % mod); return 0; }
|