版本比较
[SP CNT3]
正文
增加 108 行删除 5 行
1234556789113 行未改动全部展开12312412512612712712812913013172 行未改动20420520620720820820921021121260 行未改动273274275276277278279280281282281283284285284286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394# 组合计数选讲 3
## 集合划分容斥
:::epigraph[我 in 2026-07-14 21:19]:::epigraph[我 at 2026-07-14 21:19]我不会。:::
来个简单易懂小例子做引入。113 行未改动全部展开不过这个时候又发现和上面一样的问题,就是实际情况还原到按住情况,点集可能会分裂。
难住你了?
仿照上文,注意这次分裂出来的点集**没有顺序**,发现 $g(i)=1$,所以仿照上文,注意这次分裂出来的点集**有顺序**,发现 $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}72 行未改动设 $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)} f_{S\backslash T} 2^{|E(T, T)|} \\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$ 即可。 60 行未改动 - $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$ 连通,原因是实际情况还原到按住情况依旧会裂开。 单个连通块的容斥系数是什么?自然是可能的 $(-1)^{|E|}$ 之和,要求 $E$ 端点全在 $S$ 内,且使得 $S$ 连通,原因是实际情况还原到按住情况依旧会裂开。 直接搜索划分没法过,所以按 $a_i$ 从小往大排序,再看搜索划分过程,转移时枚举等价类内最小元即可。
时间复杂度 $\Theta(n(3^n+2^n\log V))$。时间复杂度 $\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}$,反解即可。
