CodeForces 1784D Wooden Spoon
我怎么连这种 combinatorics 都不会
题目中的关系可以写成 \(1 = x_1 < x_2 < \cdots < x_n < x_{n+1}\) 表示 \(x_i\) 打败 \(x_{i+1}\)。考虑设 \(f_{i,j}\) 为 \(x_i = j\),自顶向下考虑。
转移就是每次考虑获胜的那棵子树的贡献。有转移:
\[f_{i,j} \gets 2 \times (2^{n-i})! \times \binom{2^n - 2^{n-i} - j}{2^{n-i} - 1} \times \sum\limits_{k=1}^{j-1} f_{i-1,k}
\]
表示 \(x_i\) 和 \(x_{i+1}\) 在儿子的顺序中可以任意排列,获胜子树中在 \((j, 2^n]\) 中选 \(2^{n-i}\) 个不能和 \(x_{i+1}\) 子树中相同的数。可以前缀和优化至 \(O(n2^n)\)。
最后计算 \(i\) 的答案时就枚举这一轮的胜者,答案为 \(\sum\limits_{j=1}^{i-1} f_{n,j}\)。
code
// Problem: D. Wooden Spoon
// Contest: Codeforces - VK Cup 2022 - Финальный раунд (Engine)
// URL: https://codeforces.com/problemset/problem/1784/D
// Memory Limit: 512 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 1050000;
const int N = 1049000;
const ll mod = 998244353;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
ll n, fac[maxn], ifac[maxn], f[22][maxn], g[22][maxn];
void init() {
fac[0] = 1;
for (int i = 1; i <= N; ++i) {
fac[i] = fac[i - 1] * i % mod;
}
ifac[N] = qpow(fac[N], mod - 2);
for (int i = N - 1; ~i; --i) {
ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
}
inline ll C(ll n, ll m) {
if (n < m || n < 0 || m < 0) {
return 0;
} else {
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
}
void solve() {
scanf("%lld", &n);
int m = (1 << n);
f[1][1] = C(m - 1 - (1 << (n - 1)), (1 << (n - 1)) - 1) * 2 % mod * fac[1 << (n - 1)] % mod;
for (int i = 1; i <= m; ++i) {
g[1][i] = f[1][1];
}
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
f[i][j] = fac[1 << (n - i)] * 2 % mod * C(m - (1 << (n - i)) - j, (1 << (n - i)) - 1) % mod * g[i - 1][j - 1] % mod;
g[i][j] = (g[i][j - 1] + f[i][j]) % mod;
}
}
for (int i = 1; i <= m; ++i) {
printf("%lld\n", g[n][i - 1]);
}
}
int main() {
init();
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}