快速沃尔什变换(FWT)
位运算卷积
- OR 卷积:
\displaystyle c_k = \sum_{i | j = k} a_i \cdot b_j 。 - AND 卷积:
\displaystyle c_k = \sum_{i \& j = k} a_i \cdot b_j 。 - XOR 卷积:
\displaystyle c_k = \sum_{i \oplus j = k} a_i \cdot b_j 。
其中数组长度均为
FWT 整体思想
快速沃尔什变换(Fast Walsh-Hadamard Transform,FWT)采用“正变换 → 对应点乘 → 逆变换”的三步法:
- 对数组
a 和b 分别做 FWT 正变换,得到\mathrm{FWT}(a) 和\mathrm{FWT}(b) 。 - 对应位置相乘:
\mathrm{FWT}(c)_k = \mathrm{FWT}(a)_k \times \mathrm{FWT}(b)_k 。 - 对
\mathrm{FWT}(c) 做 FWT 逆变换,得到卷积结果c 。
三种卷积对应三种变换
- OR 卷积:子集和变换(Zeta 变换)
- AND 卷积:超集和变换
- XOR 卷积:沃尔什-哈达玛变换(分治)
OR 卷积(并集卷积)
卷积定义
正变换
OR 卷积的正变换,就是我们已经学过的子集和 SOS DP:
逆变换
子集反演:
其中
AND 卷积(交集卷积)
卷积定义
正变换
AND 卷积的正变换,就是我们已经学过的超集和 SOS DP:
逆变换
XOR 卷积
卷积定义
XOR 卷积无法直接用 SOS DP 解释,我们通过分治构造来推导变换。
分治推导与变换构造
设数组长度为
设
-
计算
c_0 (结果最高位为0 ):异或最高位为0 ,说明两个数最高位相同:- 都为
0 :a_0 和b_0 做低n - 1 位 XOR 卷积。 - 都为
1 :a_1 和b_1 做低n - 1 为 XOR 卷积。
因此:
c_0 = a_0 \oplus b_0 + a_1 \oplus b_1 。 - 都为
-
计算
c_1 (结果最高位为1 ):异或最高位为1 ,说明两个数最高位不同:- 前
0 后1 :a_0 和b_1 做低n - 1 位 XOR 卷积。 - 前
1 后0 :a_1 和b_0 做低n - 1 为 XOR 卷积。
因此:
c_0 = a_0 \oplus b_1 + a_1 \oplus b_0 。 - 前
我们希望找到变换,使得
计算变换后前后两部分的点乘:
也就是说,正变换后的点乘结果等于
例题
P4717 【模板】快速莫比乌斯 / 沃尔什变换 (FMT / FWT)
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using V = vector<LL>;
const int M = 998244353;
LL n;
V operator*(V a, V b) {
for (int i = 0; i < n; i++) a[i] = a[i] * b[i] % M;
return a;
}
V FWT(V f, int o, LL v = 1) {
for (int l = 2; l <= n; l <<= 1) {
int k = (l >> 1);
for (int i = 0; i < n; i += l)
for (int j = 0; j < k; j++) {
LL x = f[i + j];
!o && (f[i + j + k] = (f[i + j + k] + x * v + M) % M);
o == 1 && (f[i + j] = (x + f[i + j + k] * v + M) % M);
if (o == 2) {
LL y = f[i + j + k];
f[i + j] = ((x + y) % M * v % M + M) % M;
f[i + j + k] = ((x - y + M) % M * v % M + M) % M;
}
}
}
return f;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n, n = 1 << n;
V a(n), b(n), f;
for (LL &x : a) cin >> x;
for (LL &x : b) cin >> x;
f = FWT(FWT(a, 0) * FWT(b, 0), 0, -1);
for (LL x : f) cout << x << " ";
cout << "\n";
f = FWT(FWT(a, 1) * FWT(b, 1), 1, -1);
for (LL x : f) cout << x << " ";
cout << "\n";
f = FWT(FWT(a, 2) * FWT(b, 2), 2, (M + 1) / 2);
for (LL x : f) cout << x << " ";
return 0;
}
:::
CF662C Binary Table
设行的翻转情况为
设状态为
使用 XOR 卷积即可求出每个
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using V = vector<LL>;
int n, m;
string s[20];
V operator*(V a, V b) {
for (int i = 0; i < (1 << n); i++) a[i] *= b[i];
return a;
}
V FWT(V f, int v = 1) {
for (int l = 2; l <= (1 << n); l <<= 1) {
int k = (l >> 1);
for (int i = 0; i < (1 << n); i += l)
for (int j = 0; j < k; j++) {
LL x = f[i + j], y = f[i + j + k];
f[i + j] = (x + y) / v, f[i + j + k] = (x - y) / v;
}
}
return f;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
V a(1 << n, 0), b;
for (int j = 0; j < m; j++) {
int p = 0;
for (int i = 0; i < n; i++) p = (p << 1) + s[i][j] - '0';
a[p]++;
}
for (int i = 0; i < (1 << n); i++) {
int cnt = __builtin_popcount(i);
b.push_back(min(cnt, n - cnt));
}
V c = FWT(FWT(a) * FWT(b), 2);
cout << *min_element(c.begin(), c.end());
return 0;
}
:::
P1539 [TJOI2011] 01矩阵
设
变为一个 SOS DP。当
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 230, M = 1e4 + 7;
int n, m, k, f[N][1 << 15];
char a[N][N], b[N][N];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> b[i][j], a[i + 1][j] = b[i][j];
}
}
if (n < m) {
swap(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) a[i + 1][j] = b[j][i];
}
}
k = (1 << m), fill(f[0], f[0] + k, 1);
for (int i = 1; i <= n; i++) {
auto Dfs = [&](auto sf, int x, int s) {
if (x >= m) {
f[i][s] = f[i - 1][(k - 1) ^ s];
return;
}
vector<bool> t;
if (a[i][x] == '.') t.push_back(0), t.push_back(1);
else t.push_back(a[i][x] - '0');
for (bool p : t) {
!(p && x && ((s >> x - 1) & 1)) && (sf(sf, x + 1, s + (p << x)), 1);
}
};
Dfs(Dfs, 0, 0);
for (int j = 0; j < m; j++) {
for (int s = 0; s < k; s++) {
(s >> j) & 1 && (f[i][s] = (f[i][s] + f[i][s ^ (1 << j)]) % M);
}
}
}
cout << f[n][k - 1];
return 0;
}
:::