[SP CNT3]

· · 算法·理论

组合计数选讲 3

集合划分容斥

:::epigraph[我 at 2026-07-14 21:19] 我不会。 :::

来个简单易懂小例子做引入。

[ABC236Ex] Distinct Multiples

给定 N, M 和数组 \{D_N\},求有多少个正整数数组 \{A_N\} 满足:

:::info[Sol] 首先发现,没有第三条限制的情况是容易处理的。 对于第三条限制,考虑如果有一些数相等,会如何呢? 事实上,按“等于”这个关系连边,数组的位置被划分成若干“等价类”,即连通块。单个连通块的方案数是好求出的,为 $\lfloor\frac{M}{\operatorname{lcm}}\rfloor$。 如果你还是按连通块个数做斯特林反演,你就错了,这是因为:实际情况还原回按住情况,连通块会**分裂**,而不是**合并**。 合并的系数确实是第二类斯特林数,但是分裂的系数取决于连通块大小!有以下两个例子: ![](https://cdn.luogu.com.cn/upload/image_hosting/6420s1ky.png) 可以看出,方案数跟**连通块大小**还有关系! 那么,我们有其它方法吗? 考虑到连通块之间互相独立,那么,对于一张图,它对答案产生贡献,当且仅当所有连通块大小均为 1。 我们“按住”一些边,会分裂连通块,相当于直接划分。 对于大小为 $i$ 的普通图,其容斥系数和为 $g(i) = [i=1]$。不妨考虑大小为 $i$ 的,被我们按住的连通块,其容斥系数和为 $f(i)$。 对普通图枚举最小编号点所在连通块,有: $$$ \begin{aligned} g(i) &= \sum_{j=1}^{i} \binom{i-1}{j-1} f(j)g(i-j) \\ f(i) &= g(i) - \sum_{j=1}^{i-1} \binom{i-1}{j-1} f(j)g(i-j) \\ &= g(i) - (i-1)f(i-1) \\ &= (-1)^{i-1}(i-1)! \end{aligned} $$$ 然后状压转移,枚举子集,就,做完了。 相信有聪明的小朋友发现 $f(i) = (-1)^{i-1}{i \brack 1}$,其实这个是 Rota 的偏序集上 Möbius 反演,我也看不懂。 ::: :::info[Code] ```cpp line-numbers #include <bits/stdc++.h> using std::cin; using std::cout; using ll = long long; constexpr int N = 16, S = 1 << N, Mod = 998244353; int n; ll m, fac[N], d[N], w[S], dp[S]; int main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; fac[0] = 1; for (int i = 1; i < n; ++i) fac[i] = fac[i - 1] * i % Mod; for (int i = 0; i < n; ++i) cin >> d[i]; // 按住相等的贡献 w[0] = 1; for (int b = 1; b < (1 << n); ++b) { auto x = w[b & (b - 1)]; auto i = __builtin_ctz(b); if (x > m) { w[b] = m + 1; } else { auto g = x / std::__gcd(x, d[i]); w[b] = (g > m / d[i] ? m + 1 : g * d[i]); } } // 乘上容斥系数 (-1)^{|S|-1}(|S|-1)! = (-1)^{|S|-1}Si(|S|,1) for (int b = 1; b < (1 << n); ++b) { auto s = __builtin_popcount(b); w[b] = m / w[b] % Mod * (s & 1 ? 1 : Mod - 1) % Mod * fac[s - 1] % Mod; } /** * 以下部分也可以写子集卷积,做到 \Theta(N^2 2^N)。 * 怕大家看不懂 FWT 不好水紫题!!! **/ dp[0] = 1; for (int p = 1; p < (1 << n); ++p) for (int q = p; q; q = (q - 1) & p) if ((p & -p) == (q & -q)) dp[p] = (dp[p] + dp[p ^ q] * w[q]) % Mod; cout << dp[(1 << n) - 1]; } ``` ::: 怎样转到一般图呢? ### [P6846 [CEOI 2019] Amusement Park](https://www.luogu.com.cn/problem/P6846) 给一个简单有向图。对某个反转边方案,如果反转后该图变为 DAG 则称这个方案合法。对所有合法反转边方案,求反转边个数和。 $n\leq 18$。 :::info[小转化] 某方案如果合法,全体再反转一次也合法。所以求合法方案个数,乘上边数再除以二就好了。 ::: :::info[Sol] 给 DAG 做拓扑排序一定要删空。 看上去很将军,但是不妨倒过来想,每次加一个入度为 0 的独立集就好了呀! 不过这个时候又发现和上面一样的问题,就是实际情况还原到按住情况,点集可能会分裂。 难住你了? 仿照上文,注意这次分裂出来的点集**有顺序**,发现 $g(i)=1$,所以 $$$ \begin{aligned} g(i) &= \sum_{j=1}^{i} \binom{i}{j} f(j)g(i-j) \\ \texttt{I guess }f(i) &= (-1)^{i-1} \end{aligned} $$$ 由于这个太容易看出来,详细推导请自行进行子集反演,题解区有一个。 ::: :::info[Code] ```cpp line-numbers #include <bits/stdc++.h> using std::cin; using std::cout; using ll = long long; constexpr int N = 18, S = 1 << N, Mod = 998244353, Half = (Mod + 1) / 2; inline constexpr int bitcrz(int x) { return __builtin_ctz(1u * x); } inline constexpr int remlow(int x) { return x & (x - 1); } inline constexpr int parity(int x) { return __builtin_parity(1u * x); } inline void add(ll& x, ll y) { (x += y) >= Mod && (x -= Mod); } int n, m, g[N]; bool ind[S]; ll dp[S]; int main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; for (int i = 1, u, v; i <= m; ++i) { cin >> u >> v; --u; --v; g[u] |= (1 << v); g[v] |= (1 << u); } ind[0] = true; dp[0] = 1; for (int s = 1; s < (1 << n); ++s) { ind[s] = (ind[remlow(s)] && !(g[bitcrz(s)] & s)); for (int t = s; t; t = (t - 1) & s) if (ind[t]) add(dp[s], parity(t) ? dp[s ^ t] : Mod - dp[s ^ t]); } cout << dp[(1 << n) - 1] * m % Mod * Half % Mod; } ``` ::: ### [P11714 [清华集训 2014] 主旋律](https://www.luogu.com.cn/problem/P11714) 该来的还是要来。 给一个简单有向图,求边集有多少个子集构成强连通图。 $n\leq 15$。 :::info[Sol] 强连通图缩 SCC 后大小一定要是 1。 那么依旧考虑对缩 SCC 后的 DAG 计数,每次加一个独立集。 数点集 $S$ 强连通的方案数 $f_S$,那么要减去 DAG 至少两个点的情况。 设 $g_S$ 为点集 $S$ 缩为独立集的方案数,那么 $$$ \begin{aligned} f_S = 2^{|E(S, S)|} - \sum_{T\subseteq S} (-1)^{???}g_T 2^{|E(S, S\backslash T)|}+f_S \end{aligned} $$$ 加上 $f_S$ 的原因是 $T=S$ 会把缩成一个点的方案减掉,我们简单加回来。 呃,合着 $g_T$ 要带系数 $(-1)^{|V'|-1}$。 设 $g_S$ 为点集 $S$ 缩为奇数个独立集的方案数,减去偶数个的方案数,那么 $$$ \begin{aligned} f_S &= 2^{|E(S, S)|} - \sum_{T\subseteq S} g_T 2^{|E(S, S\backslash T)|}+f_S \\ g_S &= f_S - \sum_{T\subsetneq S \land \operatorname{lowbit}(S) = \operatorname{lowbit}(T)} g_{S\backslash T} 2^{|E(T, T)|} \\ f_S &= 2^{|E(S, S)|} - \sum_{T\subsetneq S} g_T 2^{|E(S, S\backslash T)|} + (f_S - g_S) \\ \end{aligned} $$$ 先算 $f_S - g_S$ 更新 $f_S$ 再更新 $g_S$ 即可。 至于 $2^{|E|}$ 属于状压基本功,固定 lowbit,不讲。 时间复杂度 $\Theta(3^n)$。 ::: :::info[Code] ```cpp #include <bits/stdc++.h> using std::cin; using std::cout; using ll = long long; constexpr int N = 15, S = 1 << N, Mod = 1e9 + 7; inline constexpr int bitcrz(int x) { return __builtin_ctz(1u * x); } inline constexpr int lowbit(int x) { return x & -x; } inline constexpr int remlow(int x) { return x & (x - 1); } inline constexpr int popcnt(int x) { return __builtin_popcount(1u * x); } inline void add(ll& x, ll y) { (x += y) >= Mod && (x -= Mod); } int n, m, in[N], out[N], es[S], ec[S]; ll P2[N * N], g[S], dp[S]; int main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m; P2[0] = 1; for (int i = 1; i <= m; ++i) P2[i] = 2 * P2[i - 1] % Mod; for (int i = 1, u, v; i <= m; ++i) { cin >> u >> v; --u; --v; out[u] |= (1 << v); in[v] |= (1 << u); } for (int s = 1; s < (1 << n); ++s) { { auto u = bitcrz(s); es[s] = es[remlow(s)] + popcnt(s & in[u]) + popcnt(s & out[u]); } ec[s] = 0; for (int t = remlow(s); t; t = (t - 1) & s) { auto o = s ^ t, u = bitcrz(o); ec[t] = ec[t ^ (1 << u)] + popcnt(t & in[u]) - popcnt(o & out[u]); } for (int t = remlow(s); t; t = (t - 1) & s) if (lowbit(s) == lowbit(t)) add(g[s], dp[t] * g[s ^ t] % Mod); g[s] = (g[s] ? Mod - g[s] : 0); for (int t = s; t; t = (t - 1) & s) add(dp[s], g[t] * P2[es[s ^ t] + ec[t]] % Mod); add(g[s], dp[s] = (P2[es[s]] + Mod - dp[s]) % Mod); } cout << dp[(1 << n) - 1]; } ``` ::: ### [P10104 [GDKOI2023 提高组] 异或图](https://www.luogu.com.cn/problem/P10104) 给定一张 $n$ 个点 $m$ 条边的无向图和数组 $\{a_n\}$ 以及整数 $C$,求有多少个数组 $\{b_n\}$ 满足: - $0 ≤ b_i ≤ a_i,\forall 1 ≤ i ≤ n$; - $\bigoplus_{i=1}^n b_i = C$; - 对于每条边 $(u, v)$,$b_u \ne b_v$。 $n\leq 15$。 :::info[Sol] 对于 $m=0$,你要会做 [P3447 [POI 2006] KRY-Crystals](https://www.luogu.com.cn/problem/P3447)。 枚举连通块划分,则分奇偶讨论,都只关心块内最小值。 单个连通块的容斥系数是什么?自然是可能的 $(-1)^{|E|}$ 之和,要求 $E$ 端点全在 $S$ 内,且使得 $S$ 连通,原因是实际情况还原到按住情况依旧会裂开。 直接搜索划分没法过,所以按 $a_i$ 从小往大排序,再看搜索划分过程,转移时枚举等价类内最小元即可。 时间复杂度 $\Theta(3^n+2^n\log V)$。 ::: :::info[Code] ```cpp #include <bits/stdc++.h> namespace Hy { using std::cin; using std::cout; using ull = std::uint64_t; constexpr int N = 15, S = 1 << N, B = 61, Mod = 998244353, Half = (Mod + 1) >> 1; inline void Add(ull& x, ull y) { (x += y) >= Mod && (x -= Mod); } int n, m, g[N], _p[N], _q[N]; bool ind[S], ff[S]; ull pinv[B], C, a[N], se[S], w[2][S], dp[S][2], af[S], Ans; int my_main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> m >> C; pinv[0] = 1; for (int i = 1; i < B; ++i) pinv[i] = pinv[i - 1] * Half % Mod; for (int i = 0; i < n; ++i) cin >> a[i], ++a[i]; std::iota(_p, _p + n, 0); std::sort(_p, _p + n, [](int l, int r) { return a[l] < a[r]; }); for (int i = 0; i < n; ++i) _q[_p[i]] = i; std::sort(a, a + n); for (int i = 0, u, v; i < m; ++i) { cin >> u >> v; u = _q[u - 1], v = _q[v - 1]; g[u] |= 1 << v; g[v] |= 1 << u; } se[0] = ind[0] = 1; for (int p = 1; p < (1 << n); ++p) { ind[p] = ind[p & (p - 1)] && !(g[__builtin_ctz(p)] & p); se[p] = ind[p]; for (int q = (p - 1) & p; q; q = (q - 1) & p) if ((p & -p) == (q & -q) && ind[p ^ q]) Add(se[p], Mod - se[q]); } w[0][0] = 1; for (int i = 0; i < n; ++i) { auto &lst = w[i & 1], &now = w[~i & 1]; std::fill(now, now + (1 << n), 0); for (int c = 0; c < (1 << i); ++c) for (int b = 1 << (n - i - 1), p = 0; p < b; ++p) { auto pre = c ^ (p << (i + 1)); Add(now[pre], lst[pre ^ (1 << i)]); for (int un = p ^ (b - 1), q = un; ; q = (q - 1) & un) { auto s = (1 << i) ^ (q << (i + 1)); auto prt = __builtin_parity(q) ^ 1; auto nxt = c ^ (prt << i) ^ ((p ^ q) << (i + 1)); Add(now[nxt], se[s] * lst[pre] % Mod * (prt ? 1 : a[i] % Mod) % Mod); if (!q) break; } } } auto &fin = w[n & 1]; std::fill(ind, ind + (1 << n), true); for (int b = B; b--;) { dp[0][0] = 1; dp[0][1] = 0; ff[0] = 0; af[0] = 1; auto p2 = (1ull << b) % Mod; for (int p = 1; p < (1 << n); ++p) { auto i = __builtin_ctz(p), q = p & (p - 1); auto &lst = dp[q], &now = dp[p]; auto wn = (a[i] & ((1ull << b) - 1)) % Mod; if ((a[i] >> b) & 1) { ff[p] = ff[q] ^ 1; now[0] = (lst[0] * p2 + lst[1] * wn) % Mod; now[1] = (lst[1] * p2 + lst[0] * wn) % Mod; } else { ff[p] = ff[q]; now[0] = lst[0] * wn % Mod; now[1] = lst[1] * wn % Mod; } af[p] = af[q] * wn % Mod; auto f = (C >> b) & 1; if (ind[p]) Ans = (Ans + fin[p] * (now[f] + (f == ff[p] ? Mod - af[p] : 0)) % Mod * pinv[b]) % Mod; if (f != ff[p]) ind[p] = false; } } cout << Ans; return 0; } } // namespace Hy int main() { return Hy::my_main(); } ``` ::: ### 总结 计数若需要划分等价类,通常其系数 $F$ 只与其大小有关。 “按住”这一操作,连通块对应系数 $G$ 也只需与其大小有关。 描绘实际情况还原到按住情况,有 $F = \frac{1}{1-G}$,反解即可。 ![](https://cdn.luogu.com.cn/upload/image_hosting/8ixz4qj9.png)