SOS DP
子集和问题
给定一个长度为
这个问题称为子集和问题(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 算法原理
定义二维状态
设从
-
$$f_{k + 1, \mathrm{mask}} = f_{k, \mathrm{mask}}$$ -
$$f_{k + 1, \mathrm{mask}} = f_{k, \mathrm{mask}} + f_{k, \mathrm{mask} \oplus 2 ^ k}$$
初始状态:
滚动数组优化
计算第
性质:当处理第
因此,从大到小枚举
超集和 DP
与子集和相对应的是超集和问题:对于每个
转移方程:思路和子集和相对称:按位处理,决策“这一位是否可以多加一个
- 如果
\mathrm{mask} 的第k 位是1 :超集的第k 位必须是1 ,结果不变。 - 如果
\mathrm{mask} 的第k 位是0 :超集的第k 位可以是0 或1 ,加上第k 位为1 的情况。
例题
CF165E Compatible Numbers
模板题。用
:::success[代码]
#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
对每个
:::success[代码]
#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
设
最后每一个
:::success[代码]
#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[代码]
#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 逆变换求出答案。
:::success[代码]
#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;
}
:::