跳到正文

题解

ABC253Ex 题解

Register_int约 5 分钟
原文
文章信息与版本

文章信息

发布
2023年1月8日
收录
2026年11月4日
更新
2026年11月20日
篇幅
约 5 分钟
获赞
12

版本

  1. v1当前

首先选 ii 步的总方案数是 mim^i,所以直接把方案数算出来,再一除就是概率了。

看到 n≤14n\le14 就知道复杂度绝对带一个 3n3^n,直接子集 dp。设 dpi,Sdp_{i,S} 表示 ii 步后点集 SS 为森林的方案数。显然可以让一个子集裂开,自己变成一棵树。再用上固定基准点的经典套路,得到转移方程:

dpi,S=∑S1∈T⊆Sdpi−∣T∣+1,S/T×fTdp_{i,S}=\sum_{S_1\in T\subseteq S}dp_{i-|T|+1,S/T}\times f_T

其中,fTf_T 表示让点集 TT 成为一颗树的方案数。显然可以得知,当 ∣S∣=1|S|=1 时,fS=1f_S=1。然而还不是很好处理,此时运用树的一个性质:断开任意一条边后树会裂开成两棵树。那么继续枚举子集裂开,得到:

fS=∑T⊂SfS/T×fT×edgeS/T,Tf_S=\sum_{T\subset S}f_{S/T}\times f_T\times edge_{S/T,T}

然而这样是不行的,会有很多重复的情况。对于每一种方案,考虑其中的一条边,设断开该边后组成的树为 TT 和 S/TS/T。显然,该方案会被 T,S/TT,S/T 与 S/T,S/(S/T)=TS/T,S/(S/T)=T 计算两遍,树中总计 ∣S∣−1|S|-1 条边,计算次数就是 2(∣S∣−1)2(|S|-1),除掉即可。

最后处理边数。暴力与处理绝对不行,考虑继续容斥(套了三层了已经),边数为两个子集并的内部连边数减去两个子集内部的边数,即:

edgeS,T=edgeS∪T−edgeS−edgeTedge_{S,T}=edge_{S\cup T}-edge_S-edge_T

暴力预处理每个子集的答案即可。最后,设 VV 为所有点组成的点集,由于没有指定加入 ii 条边的顺序,所以方案数为:

dpi,V×i!dp_{i,V}\times i!

概率为:

dpi,V×i!mi\dfrac{dp_{i,V}\times i!}{m^i}

完结撒花!

AC 代码

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 3e3 + 10;
const int mod = 998244353;

inline 
ll inv(ll b, ll p = mod - 2) {
	ll res = 1;
	while (p) {
		if (p & 1) res = res * b % mod;
		b = b * b % mod, p >>= 1;
	}
	return res;
}

int n, m; ll p = 1;

int u[510], v[510], cnt[1 << 14];

ll num[1 << 14], f[1 << 14], dp[14][1 << 14], fac[14];

inline 
ll calc(int s, int t) {
	return num[s | t] - num[s] - num[t];
}

int main() {
	scanf("%d%d", &n, &m), *f = *fac = 1;
	for (int i = 1; i <= m; i++) scanf("%d%d", &u[i], &v[i]), u[i]--, v[i]--;
	for (int i = 1; i < n; i++) fac[i] = fac[i - 1] * i % mod;
	for (int s = 0; s < 1 << n; s++) {
		for (int i = 1; i <= m; i++) if (s >> u[i] & 1 && s >> v[i] & 1) num[s]++;
		if (s) cnt[s] = cnt[s ^ s & -s] + 1;
	}
	for (int s = 1; s < 1 << n; s++) {
		if (s == (s & -s)) { f[s] = 1; continue; }
		for (int t = s - 1 & s; t; t = t - 1 & s) {
			f[s] = (f[s] + f[s ^ t] * f[t] % mod * calc(s ^ t, t) % mod) % mod;
		}
		f[s] = f[s] * inv(cnt[s] - 1 << 1) % mod;
	}
	for (int i = 0; i < n; i++) {
		for (int s = 0, k, x; s < 1 << n; s++) {
			if (cnt[s] - 1 == i) dp[i][s] = f[s];
			if (!s) continue;
			x = s & -s, k = s ^ x;
			for (int t = k - 1 & k; t != k; t = t - 1 & k) {
				if (cnt[t] <= i) dp[i][s] = (dp[i][s] + dp[i - cnt[t]][s ^ t ^ x] * f[t ^ x] % mod) % mod;
			}
		}
	}
	for (int i = 1; i < n; i++) p = p * m % mod, printf("%lld\n", dp[i][(1 << n) - 1] * fac[i] % mod * inv(p) % mod);
}
原文