子集和问题
给定一个长度为 的数组 ,下标对应的二进制状态 ,我们需要对每一个 ,计算它的所有子集的权值之和,即:
这个问题称为子集和问题(Sum Over Subsets),是状压算法中经典的基础问题之一。
常规解法及其复杂度
最直接的做法是:枚举每一个 ,在枚举它的所有子集并累加。
定理:若 是 的非空子集,则 是 的子集,且是字典序意义下比 小的下一个子集。
枚举子集经典写法:
for (int mask = 0; mask < (1 << n); mask++) {
f[mask] = 0;
for (int s = mask;; s = (s - 1) & mask) {
f[mask] += a[s];
if (!s) break;
}
}复杂度分析:对于有 个 的 ,它有 个子集,总操作次数为
故时间复杂度为 。
我们需要一种更高效的算法,将时间复杂度从 将为更低的复杂度,SOS DP 就是为此而生的,它可以在 的时间复杂度内解决子集和问题。
SOS DP 算法原理
定义二维状态 :只考虑二进制的前 位时, 的所有子集的权值之和,这些子集只能在前 位上与 不同。
设从 到 转移,也就是加入第 位的决策。对于任意 ,分两种情况:
- 的第 位是 :子集的第 位也只能是 ,因此前 位的结果和前 位的结果相同:
- 的第 位是 :子集的第 位(对应前 位)可以选 ,也可以选 ,两者相加就是总子集和:
初始状态:,答案:。
滚动数组优化
计算第 层的答案时只需要第 层的值,因此可以去掉第一维。
性质:当处理第 位时,所有第 位为 的 ,值不会发生改变;而第 位为 的 ,需要加上 的值——这个数第 位是 ,本轮没有被修改过,还是上一轮的值。
因此,从大到小枚举 是完全正确的。
超集和 DP
与子集和相对应的是超集和问题:对于每个 ,计算所有包含它的集合的权值之和:
转移方程:思路和子集和相对称:按位处理,决策“这一位是否可以多加一个 ”。
- 如果 的第 位是 :超集的第 位必须是 ,结果不变。
- 如果 的第 位是 :超集的第 位可以是 或 ,加上第 位为 的情况。
例题
CF165E Compatible Numbers
模板题。用 记录 包含的任意一个 的值,查询的答案就是 。
代码
#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
对每个 记录下其子集的最大值和次大值,答案就是 中最大的最大值加次大值。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using Pii = pair<LL, LL>;
#define fir first
#define sec second
LL 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
设 表示集合为 的这些位置上至少有一个 的数量,那么转化为了一个超集和 DP 的问题。
最后每一个 产生的贡献为 ,用容斥计算答案即可:
代码
#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
和上一题几乎是一样的,只需要改一点。
代码
#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;
}子集反演和超级反演
子集反演公式
如果我们已知子集和数组 ,想反推出原数组 ,这就是子集反演:
其中 表示 的二进制中的 的个数。
逆 SOS DP
和正向 SOS DP 相对称,改为
超集反演公式
练习
CodeChef - COVERING Covering Sets
设 ,,。
构造 ,其表示
而我们要求恰好相等的,所以可以利用 SOS DP 逆变换求出答案。
代码
#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;
}