正文
增加 235 行删除 0 行
50 行未改动5152535450 行未改动**性质**:当处理第 $k$ 位时,所有第 $k$ 位为 $0$ 的 $\mathrm{mask}$,值不会发生改变;而第 $k$ 位为 $1$ 的 $\mathrm{mask}$,需要加上 $\mathrm{mask} \oplus 2 ^ k$ 的值——这个数第 $k$ 位是 $0$,本轮没有被修改过,还是上一轮的值。因此,从大到小枚举 $\mathrm{mask}$ 是完全正确的。50 行未改动5152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828950 行未改动**性质**:当处理第 $k$ 位时,所有第 $k$ 位为 $0$ 的 $\mathrm{mask}$,值不会发生改变;而第 $k$ 位为 $1$ 的 $\mathrm{mask}$,需要加上 $\mathrm{mask} \oplus 2 ^ k$ 的值——这个数第 $k$ 位是 $0$,本轮没有被修改过,还是上一轮的值。因此,从大到小枚举 $\mathrm{mask}$ 是完全正确的。## 超集和 DP与子集和相对应的是超集和问题:对于每个 $\mathrm{mask}$,计算所有包含它的集合的权值之和:$$g_{\mathrm{mask}} = \sum_{s \supseteq \mathrm{mask}} a_s$$转移方程:思路和子集和相对称:按位处理,决策“这一位是否可以多加一个 $1$”。+ 如果 $\mathrm{mask}$ 的第 $k$ 位是 $1$:超集的第 $k$ 位必须是 $1$,结果不变。+ 如果 $\mathrm{mask}$ 的第 $k$ 位是 $0$:超集的第 $k$ 位可以是 $0$ 或 $1$,加上第 $k$ 位为 $1$ 的情况。## 例题### CF165E Compatible Numbers模板题。用 $f_{\mathrm{mask}}$ 记录 $\mathrm{mask}$ 包含的任意一个 $a_i$ 的值,查询的答案就是 $f_{(2 ^ m - 1) \oplus a_i}$。:::success[代码]```cpp#include <bits/stdc++.h>using namespace std;using LL = long long;const int N = 1e6 + 5, M = 1 << 22;LL n, a[N], f[M];int main() {cin.tie(0)->sync_with_stdio(0);cin >> n;fill(f, f + M, -1);for (int i = 1; i <= n; i++) cin >> a[i], f[a[i]] = a[i];for (int i = 0; i < 22; i++) {for (int s = 0; s < M; s++) {(s >> i) & 1 && (f[s] = max(f[s], f[s ^ (1 << i)]));}}for (int i = 1; i <= n; i++) {cout << f[(M - 1) ^ a[i]] << " ";}return 0;}```:::### AT_arc100_c [ARC100E] Or Plus Max对每个 $\mathrm{mask}$ 记录下其子集的最大值和次大值,答案就是 $1 \sim k$ 中最大的最大值加次大值。:::success[代码]```cpp#include <bits/stdc++.h>using namespace std;using LL = long long;using Pii = pair<LL, LL>;#define fir first#define sec secondLL n, m, a[1 << 18];Pii f[1 << 18];void Upd(Pii &a, Pii b) {b.fir > a.fir ? a.sec = a.fir, a.fir = b.fir, 1 : a.sec = max(a.sec, b.fir);}int main() {cin.tie(0)->sync_with_stdio(0);cin >> n, m = 1 << n;for (int i = 0; i < m; i++) cin >> a[i], f[i] = {a[i], 0};for (int i = 0; i < n; i++) {for (int s = 0; s < m; s++) {(s >> i) & 1 && (Upd(f[s], f[s ^ (1 << i)]), 1);}}LL ans = 0;for (int i = 1; i < m; i++) {ans = max(ans, f[i].fir + f[i].sec);cout << ans << "\n";}return 0;}```:::### CF449D Jzzhu and Numbers设 $f_{\mathrm{mask}}$ 表示集合为 $\mathrm{mask}$ 的这些位置上至少有一个 $1$ 的数量,那么转化为了一个超集和 DP 的问题。最后每一个 $\mathrm{mask}$ 产生的贡献为 $2 ^ {f_{\mathrm{mask}}} - 1$,用容斥计算答案即可:$$\sum_{\mathrm{mask}} (-1) ^ {\mathrm{popcount}(\mathrm{mask})} \left(2 ^ {f_{\mathrm{mask}}} - 1\right)$$:::success[代码]```cpp#include <bits/stdc++.h>using namespace std;using LL = long long;const int N = 1 << 20, M = 1e9 + 7;LL n, a[N], f[N];LL Pow(LL x, LL y) {LL s = 1;for (; y; x = x * x % M, y >>= 1) y & 1 && (s = s * x % M);return s;}int main() {cin.tie(0)->sync_with_stdio(0);cin >> n;for (int i = 1; i <= n; i++) cin >> a[i], f[a[i]]++;for (int i = 0; i < 20; i++) {for (int s = 0; s < N; s++) {!((s >> i) & 1) && (f[s] += f[s ^ (1 << i)]);}}LL ans = 0;for (int s = 0; s < N; s++) {ans = (ans + (__builtin_popcount(s) % 2 ? -1 : 1) * (Pow(2, f[s]) - 1) + M) % M;}cout << ans;return 0;}```:::### P6442 [COCI 2011/2012 #6] KOŠARE和上一题几乎是一样的,只需要改一点。:::success[代码]```cpp#include <bits/stdc++.h>using namespace std;using LL = long long;const int N = 1 << 20, M = 1e9 + 7;LL n, m, a[N], f[N];LL Pow(LL x, LL y) {LL s = 1;for (; y; x = x * x % M, y >>= 1) y & 1 && (s = s * x % M);return s;}int main() {cin.tie(0)->sync_with_stdio(0);cin >> n >> m;for (int i = 1, k, x; i <= n; i++) {int s = 0;for (cin >> k; k; k--) cin >> x, s |= (1 << x - 1);f[s]++;}for (int i = 0; i < m; i++) {for (int s = 0; s < (1 << m); s++) {((s >> i) & 1) && (f[s] += f[s ^ (1 << i)]);}}LL ans = 0;for (int s = 0; s < N; s++) {ans = (ans + ((m - __builtin_popcount(s)) % 2 ? -1 : 1) * (Pow(2, f[s]) - 1) + M) % M;}cout << ans;return 0;}```:::## 子集反演和超级反演### 子集反演公式如果我们已知子集和数组 $f$,想反推出原数组 $a$,这就是子集反演:$$a_{\mathrm{mask}} = \sum_{s \subseteq \mathrm{mask}} (-1) ^ {|\mathrm{mask}| - |s|} f_s$$其中 $|x|$ 表示 $x$ 的二进制中的 $1$ 的个数。### 逆 SOS DP和正向 SOS DP 相对称,改为$$a_{\mathrm{mask}} = a_{\mathrm{mask}} - a_{\mathrm{mask} \oplus 2 ^ k} ~~ (\mathrm{mask} ~ \texttt{含第} ~ i ~ \texttt{位时})$$### 超集反演公式$$a_{\mathrm{mask}} = \sum_{s \supseteq \mathrm{mask}} (-1) ^ {|\mathrm{mask}| - |s|} f_s$$## 练习### CodeChef - COVERING Covering Sets$$R(X) = \sum_{X \subseteq A \cup B \cup C} F(A) G(B) H(C)$$$$Ans = \sum_{A, B, C} F(A) G(B) H(C) \cdot 2 ^ {|A \cup B \cup C|}$$设 $\displaystyle F_a(S) = \sum_{A \subseteq S} F(A)$,$\displaystyle G_b(S) = \sum_{B \subseteq S} G(B)$,$\displaystyle H_c(S) = \sum_{C \subseteq S} H(C)$。构造 $W(S) = F_a(S) G_b(S) H_C(S)$,其表示$$\sum_{A \cup B \cup C \subseteq S} F(A) G(B) H(C)$$而我们要求恰好相等的,所以可以利用 SOS DP 逆变换求出答案。:::success[代码]```cpp#include <bits/stdc++.h>using namespace std;using LL = long long;const int N = 1 << 20, M = 1e9 + 7;LL n, m, a[3][N], f[N];LL Pow(LL x, LL y) {LL s = 1;for (; y; x = x * x % M, y >>= 1) y & 1 && (s = s * x % M);return s;}int main() {cin.tie(0)->sync_with_stdio(0);cin >> n, m = (1 << n);for (int i = 0; i < 3; i++) {for (int j = 0; j < m; j++) cin >> a[i][j];}for (int i = 0; i < 3; i++) {for (int j = 0; j < n; j++) {for (int s = 0; s < m; s++) {(s >> j) & 1 && (a[i][s] = (a[i][s] + a[i][s ^ (1 << j)]) % M);}}}for (int s = 0; s < m; s++) {f[s] = a[0][s] * a[1][s] % M * a[2][s] % M;}for (int i = 0; i < n; i++) {for (int s = 0; s < m; s++) {(s >> i) & 1 && (f[s] = (f[s] - f[s ^ (1 << i)] + M) % M);}}LL ans = 0;for (int i = 0; i < m; i++) {ans = (ans + f[i] * Pow(2, __builtin_popcount(i)) % M) % M;}cout << ans;return 0;}```:::