跳到正文

题解

题解:CF1667E Centroid Probabilities

251Sec约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
获赞
1
题目
CF1667E

版本

  1. v1当前

你说得对,但是我口胡完 O(nlog⁡n)O(n \log n) 之后打开题解一看怎么全是线性的,这就是多项式学傻了带给我的自信。

考虑先算 f(i)f(i) 代表 ii 个点,根节点每个儿子子树大小不超过 n2\frac{n}{2}(注意这里是 nn 不是 ii) 的方案。这个很好算,每次先随便给 ii 定个父亲然后容斥掉 ii 所在子树过大的情况就行了。然后用 f(i)f(i) 算答案 ans(u)\text{ans}(u),枚举 uu 子树的大小 jj,选 j−1j-1 个 >u>u 的点放到 uu 子树里,然后给剩下的点按编号从小到大定父亲,推出来式子发现是个卷积,所以就对了。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P = 998244353, G = 3, GI = (P + 1) / 3;
int n, L;
ll fac[200005], ifac[200005], f[1000005], g[1000005];
ll QPow(ll a, ll b) {
	ll res = 1;
	for (; b; b >>= 1, a = a * a % P) if (b & 1) res = res * a % P;
	return res;
}
ll C(int n, int m) { return fac[n] * ifac[m] % P * ifac[n - m] % P; }
int r[1000005], lim, llim;
void SetLim(int x) {
	llim = __lg(x - 1) + 1, lim = 1 << llim;
	for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (llim - 1));
}
void NTT(ll *a, int tp) {
	for (int i = 0; i < lim; i++) if (i < r[i]) swap(a[i], a[r[i]]);
	for (int w = 1; w < lim; w <<= 1) {
		ll omg = QPow(tp == 1 ? G : GI, (P - 1) / (w << 1));
		for (int i = 0; i < lim; i += (w << 1)) {
			ll t = 1;
			for (int j = 0; j < w; j++) {
				ll x = a[i + j], y = a[i + j + w] * t % P;
				a[i + j] = (x + y) % P;
				a[i + j + w] = (x - y + P) % P;
				t = t * omg % P;
			}
		}
	}
	if (tp == -1) {
		ll iv = QPow(lim, P - 2);
		for (int i = 0; i < lim; i++) a[i] = a[i] * iv % P;
	}
}
int main() {
	scanf("%d", &n), L = n / 2 + 1;
	fac[0] = 1;
	for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % P;
	ifac[n] = QPow(fac[n], P - 2);
	for (int i = n; i; i--) ifac[i - 1] = ifac[i] * i % P;
	f[1] = 1;
	for (int i = 2; i <= n; i++) {
		f[i] = f[i - 1] * (i - 1) % P;
		if (i > L) {
			f[i] -= f[i - L] * f[L] % P * C(i - 2, L - 1) % P;
			f[i] = (f[i] + P) % P;
		}
	}
	printf("%lld ", f[n]);
	for (int i = 1; i < n - L + 1; i++) f[i] = 0;
	for (int i = n - L + 1; i <= n; i++) f[i] = f[i] * ifac[i - 1] % P * fac[n - i - 1] % P;
	for (int i = 0; i <= n + 1; i++) g[i] = ifac[n - i + 1];
	reverse(f, f + n + 1);
	SetLim(n * 2 + 5);
	NTT(f, 1), NTT(g, 1);
	for (int i = 0; i < lim; i++) f[i] = f[i] * g[i] % P;
	NTT(f, -1);
	for (int i = 2; i <= n; i++) {
		printf("%lld ", f[n + i] % P * fac[n - i] % P * (i - 1) % P);
	}
	return 0;
}
原文