FMT / FWT 研究笔记

· · 算法·理论

本文旨在快速帮助读者了解这两种算法。

FMT 快速莫比乌斯变换

定义

对于正整数 n 和长度为 2 ^ n 的序列 / 函数 f(S),我们将下标 S 看作 n 位二进制数。

那么有:

\operatorname{FMT}(f)(S) = \sum_{T \subset S} f(T)

逆变换就是从 \operatorname{FMT}(f) 到 f 的过程,本质上是一个高维前缀和 SOSDP。

实现

正变换

我们采用分治的思想。考虑如下过程:对于正整数 n 和序列 f,我们先对最高位为 0 和 1 的那两半部分单独做 \operatorname{FMT},最后左半部分对位加到右半部分。

形式化地,\operatorname{FMT}(f) = \operatorname{merge}(\operatorname{FMT}(f_0), \operatorname{FMT}(f_0) + \operatorname{FMT}(f_1)),其中 + 表示对位相加,\operatorname{merge} 表示合并两个序列。

实现

采用迭代实现,常数更优秀,时间复杂度 \Theta(n \cdot 2 ^ n)。

inline void FMT(int n, int *f) {
    for (int L = 2, l = 1; L <= (1 << n); L <<= 1, l <<= 1)
        for (int i = 0; i < (1 << n); i += L) for (int j = 0; j < l; ++j)
            f[i + j + l] += f[i + j];
    return void();
}

逆变换

本质上是一个高维差分的过程。

显然有 \operatorname{IFMT}(f) = \operatorname{merge}(\operatorname{IFMT}(f_0), \operatorname{IFMT}(f_1) - \operatorname{IFMT}(f_0)),同理递归。

代码合并

inline void FMT(int n, int *f, const int val) { // FMT ~ 1 IFMT ~ -1
    for (int L = 2, l = 1; L <= (1 << n); L <<= 1, l <<= 1)
        for (int i = 0; i < (1 << n); i += L) for (int j = 0; j < l; ++j)
            f[i + j + l] += f[i + j] * val;
    return void();
}

深入理解

## FMT 应用 ### 或卷积 #### 问题引入 对于正整数 $n$ 和序列 / 函数 $f, g$ 我们需要求出同样长度为 $2 ^ n$ 的序列 $h$ 满足 $h(i) = \sum_{j \operatorname{or} k = i} f(j) \cdot g(k)$,真的是卷积! #### FMT 解决方法 我们对 $f, g, h$ 做快速莫比乌斯变换得到 $f', g', h'$ 则有 $h'(i) = f'(i) \cdot g'(i)$,即满足简单点乘关系。 原理如下。 $$ h'(i) = \sum_{j \subset i} \sum_{k \operatorname{or} l = j} f(k) \cdot g(l) = f'(i) \cdot g'(i) $$ 比较容易推导,真是太巧妙了!最后对 $h'$ 做逆快速莫比乌斯变换即可得到 $h$。 ### 与卷积 前情提要:高维前缀和叫 $\operatorname{FMT}$,那我称高维后缀和为快速莫比乌斯转换 $\operatorname{FMt}$。 #### 问题背景 同上,位运算换成与 $\operatorname{and}$。 #### 解决方法 其实比较简单,毕竟后缀和、前缀和本质上一样,就不讲解了。 ### 深入理解 这个算法本质上是通过一种空间将卷积转化成线性点乘形式,关键在于空间关系的构造。 # 快速沃尔什变换 FWT ## 定义 前情提要:对于 $n$ 位二进制数 $x, y$,$x \otimes y = \operatorname{popcount}(x \operatorname{and} y) \bmod 2$。 则定义 $\operatorname{FWT}(f)(i) = \sum_{i \otimes j = 0} f(j) - \sum_{i \otimes j = 1} f(j)$。 逆变换就是逆处理,可以证明是唯一的。 ## 实现 仍然考虑分治。 $$ \operatorname{FWT}(f) = \operatorname{merge}(\operatorname{FWT}(f_0) + \operatorname{FWT}(f_1), \operatorname{FWT}(f_0) - \operatorname{FWT}(f_1)) $$ 逆变换逆推即可,同时证明了逆变换的唯一性。 ### 代码 使用 `double` 类型实现。 ```cpp inline void FWT(int n, double *f, const double val) { // 正 ~ 1 逆 ~ 0.5 for (int L = 2, l = 1; L <= (1 << n); L <<= 1, l <<= 1) for (int i = 0; i < (1 << n); i += L) for (int j = 0; j < l; ++j) f[i + j] -= f[i + j + l], f[i + j + l] = f[i + j] - f[i + j + l] * 2.00, f[i + j] *= val, f[i + j + l] *= val; } ``` ## 应用:FWT 异或卷积 ### 问题背景 前提同上,求长度为 $2 ^ n$ 的序列 $h$ 满足 $h(i) = \sum_{j \operatorname{xor} k = i} f(j) \cdot g(k)$,$n$ 一般在 $20$ 左右。 ### FWT 求解方法 我们对 $f, g, h$ 做 $\operatorname{FWT}$ 变换,则在这个空间下 $f, g, h$ 同样满足简单点乘关系,再对 $h$ 做 $\operatorname{IFWT}$ 变换即可得到答案。 # 总结 实际上背下模板基本就够用了。不过几种卷积的创新性处理方法仍具有较大启发性! 谢谢阅读。 # 例题讲解 ## [P4717 模板题](https://www.luogu.com.cn/problem/P4717) 套用以上公式即可。 ### Code ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; constexpr ll Mod = 998244353ll, Inv = 499122177ll; inline void add(ll &x, const ll y) { x = (x + y) % Mod; } inline void mul(ll &x, const ll y) { x = x * y % Mod; } const int N = 1000005; int n; ll f[N], g[N]; ll h[N]; inline void cal() { for (int i = 0; i < (1 << n); ++i) h[i] = f[i] * g[i] % Mod; return void(); } inline void out(const bool fl) { for (int i = 0; i < (1 << n); ++i) { cout << h[i]; if (i < (1 << n) - 1) cout << ' '; } if (fl) cout << '\n'; return void(); } inline void FMT(ll *a, const ll val) { for (int L = 2, l = 1; L <= (1 << n); L <<= 1, l <<= 1) for (int i = 0; i < (1 << n); i += L) for (int j = 0; j < l; ++j) add(a[i + j + l], a[i + j] * val % Mod); return void(); } inline void FMt(ll *a, const ll val) { for (int L = 2, l = 1; L <= (1 << n); L <<= 1, l <<= 1) for (int i = 0; i < (1 << n); i += L) for (int j = 0; j < l; ++j) add(a[i + j], a[i + j + l] * val % Mod); return void(); } inline void FWT(ll *a, const ll val) { for (int L = 2, l = 1; L <= (1 << n); L <<= 1, l <<= 1) for (int i = 0; i < (1 << n); i += L) for (int j = 0; j < l; ++j) add(a[i + j], a[i + j + l]), a[i + j + l] = (a[i + j] - a[i + j + l] * 2ll + Mod * 2ll) % Mod, mul(a[i + j], val), mul(a[i + j + l], val); return void(); } int main() { cin.tie(0) -> sync_with_stdio(false); cin >> n; for (int i = 0; i < (1 << n); ++i) cin >> f[i]; for (int i = 0; i < (1 << n); ++i) cin >> g[i]; FMT(f, 1ll), FMT(g, 1ll), cal(), FMT(h, Mod - 1ll), out(true), FMT(f, Mod - 1ll), FMT(g, Mod - 1ll); FMt(f, 1ll), FMt(g, 1ll), cal(), FMt(h, Mod - 1ll), out(true), FMt(f, Mod - 1ll), FMt(g, Mod - 1ll); FWT(f, 1ll), FWT(g, 1ll), cal(), FWT(h, Inv), out(false), FWT(f, Inv), FWT(g, Inv); // Still change it back to before. return 0; } ``` ## [CF449D FMt 应用题](https://codeforces.com/problemset/problem/449/D) ### Solution 不妨令 $0 \le a_i \le 2 ^ {21}$,即看作任意 $21$ 位二进制数。 考虑令 $h_S$ 表示 $S$ 的出现次数,对 $h$ 作高维后缀和 $\operatorname{FMt}$,同时令 $2 ^ {h_S} - 1 \to h_S$,则 $h_S$ 表示使得结果为 $S$ 超集的个数。那么我们再对 $h$ 做 $\operatorname{IFMt}$ 即高维后缀差分,$h_S$ 表示使得结果为 $S$ 的方案数,则 $h_0$ 即为答案。发现对 $h$ 的运算全部为线性运算,故边运算边取模即可。 这题考察了对 $\operatorname{FMt}$ 的实际意义的深刻理解。 ### Code ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; constexpr ll Mod = 1000000007ll; const int N = 8000005; int n, a[N]; ll h[N], pw[N]; inline void FMt(const ll val) { for (int L = 2, l = 1; L <= (1 << 21); L <<= 1, l <<= 1) for (int i = 0; i < (1 << 21); i += L) for (int j = 0; j < l; ++j) h[i + j] = (h[i + j] + h[i + j + l] * val + Mod) % Mod; return void(); } int main() { cin.tie(0) -> sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i], ++h[a[i]]; FMt(1ll), pw[0] = 1ll; for (register int i = 1, j = 0; i <= 8000000; ++i, ++j) pw[i] = pw[j] * 2ll % Mod; for (register int i = 0; i < (1 << 21); ++i) h[i] = (pw[h[i]] + Mod - 1ll) % Mod; FMt(Mod - 1ll), cout << h[0]; return 0; } ``` # 完结 撒花!