跳到正文

题解

题解 P4717 【【模板】快速莫比乌斯/沃尔什变换 (FMT/FWT)】

warzone约 21 分钟
原文
文章信息与版本

文章信息

发布
2021年1月28日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 21 分钟
获赞
111
题目
P4717

版本

  1. v1当前

Upd 2025/06/08:修复部分 Markdown 并调整题解中部分符号。

以下是一个偏向于初学者的题解。

卷积的定义

对于一个序列,将其中元素一一映射到一个多项式函数的系数上,
这个多项式函数便叫做该序列的 生成函数。
形式化地讲,对于序列 f0,f1,⋯ ,fnf_0,f_1,\cdots,f_n,f(x)=∑k=0nfkxkf(x)=\displaystyle\sum_{k=0}^nf_kx^k 为其生成函数。
卷积 即为生成函数的乘积在对应序列的变换上的的抽象,
“卷”即为其作用效果,“积”即为其本质。
对于序列 f,gf,g,其卷积序列 f×gf\times g 满足

(f×g)k=∑i=0kfigk−i=∑i+j=kfigj(f\times g)_k=\displaystyle\sum_{i=0}^kf_i g_{k-i}=\sum_{i+j=k}f_i g_j

FFT 计算的是循环卷积,也即 (f×g)k=∑i+j≡k(modn)figj(f\times g)_k=\displaystyle\sum_{i+j\equiv k\pmod n}f_i g_j,nn 为序列长度。

卷积的基本性质

  • f×g=g×ff\times g=g\times f
    显而易见的交换律,用定义 (f×g)k=∑i+j=kfigj(f\times g)_k=\displaystyle\sum_{i+j=k}f_i g_j,
    乘法交换律 ab=baa b=b a 即可证明。
  • (f×g)×h=f×(g×h)(f\times g)\times h=f\times(g \times h)(结合律)
    证明: (f×(g×h))n=∑a+b=nfa(g×h)b(f\times(g\times h))_n=\displaystyle\sum_{a+b=n}f_a(g\times h)_b =∑a+b=nfa∑i+j=bgihj=∑a+(i+j)=nfagihj=∑(a+i)+j=nfagihj=\sum_{a+b=n}f_a\sum_{i+j=b}g_ih_j=\sum_{a+(i+j)=n}f_ag_ih_j=\sum_{(a+i)+j=n}f_ag_ih_j 反向推导即可得证。
  • (f+g)×h=(f×h)+(g×h)(f+ g)\times h=(f\times h)+(g\times h)(分配律)
    其中 (f+g)k=fk+gk(f+ g)_k=f_k+g_k 表示向量加法。
    证明:((f+g)×h)k=∑i=0k(f+g)ihk−i=∑i=0k(fi+gi)hk−i((f+ g)\times h)_k=\displaystyle\sum_{i=0}^k(f+ g)_ih_{k-i}=\sum_{i=0}^k (f_i+g_i)h_{k-i} =∑i=0k(fihk−i+gihk−i)=∑i=0kfihk−i+∑i=0kgihk−i=(f×h)k+(g×h)k=((f×h)+(g×h))k=\sum_{i=0}^k(f_ih_{k-i}+g_ih_{k-i})=\sum_{i=0}^kf_ih_{k-i}+\sum_{i=0}^kg_ih_{k-i}\\ =(f\times h)_k+(g\times h)_k=((f\times h)+(g\times h))_k
  • (λf)×g=f×(λg)=λ(f×g)(\lambda f)\times g=f\times (\lambda g)=\lambda(f\times g)(与数乘的相容性)
    其中 (λf)k=λfk(\lambda f)_k=\lambda f_k 表示数乘。
    证明:((λf)×g)k=∑i=0k(λf)igk−i=∑i=0kλfigk−i=λ∑i=0kfigk−i=λ(f×g)k((\lambda f)\times g)_k=\displaystyle\sum_{i=0}^k(\lambda f)_ig_{k-i}=\sum_{i=0}^k\lambda f_ig_{k-i}=\lambda\sum_{i=0}^kf_ig_{k-i}=\lambda(f\times g)_k
    f×(λg)=λ(f×g)f\times (\lambda g)=\lambda(f\times g) 同理。

位运算卷积定义及其性质

一般的卷积满足 i+j=ki+j=k,又称为加法卷积。

类似的,对于序列 f={f0,f1,⋯ ,f2n−1},g={g0,g1,⋯ ,g2n−1}f=\{f_0,f_1,\cdots,f_{2^n-1}\},g=\{g_0,g_1,\cdots,g_{2^n-1}\},
可以定义位运算卷积 (f×⊙g)k=∑i⊙j=kfigj\displaystyle(f\times_\odot g)_k=\sum_{i\odot j=k}f_i g_j,其中 ⊙=and,or,xor\odot=\text{and},\text{or},\text{xor}。
还有 max⁡\max 卷积,这里不再拓展, 读者自行查找相关资料,现在着重讨论 ×xor\times_\text{xor}

  • f×xorg=g×xorf\displaystyle f\times_\text{xor} g=g\times_\text{xor} f(交换律)
    由于 axor⁡b=bxor⁡aa\operatorname{xor}b=b\operatorname{xor}a,根据定义显然成立。
    and⁡,or⁡\operatorname{and},\operatorname{or} 同样满足此性质,原因同上。
  • (f×xorg)×xorh=f×xor(g×xorh)(f\times_\text{xor} g)\times_\text{xor} h=f\times_\text{xor}(g \times_\text{xor} h)(结合律)
    证明: (f×xor(g×xorh))k=∑axor⁡b=kfa(g×xorh)b(f\times_\text{xor}(g\times_\text{xor} h))_k=\displaystyle\sum_{a\operatorname{xor}b=k}f_a(g\times_\text{xor} h)_b =∑axor⁡b=kfa∑ixor⁡j=bgihj=∑axor⁡(ixor⁡j)=kfagihj=∑(axor⁡i)xor⁡j=kfagihj=\sum_{a\operatorname{xor}b=k}f_a\sum_{i\operatorname{xor}j=b}g_ih_j=\sum_{a\operatorname{xor}(i\operatorname{xor}j)=k}f_ag_ih_j=\sum_{(a\operatorname{xor}i)\operatorname{xor}j=k}f_ag_ih_j 反向推导即可得证。and⁡,or⁡\operatorname{and},\operatorname{or} 同理。
  • (f+g)×xorh=(f×xorh)+(g×xorh)(f+ g)\times_\text{xor} h=(f\times_\text{xor} h)+(g\times_\text{xor} h)(分配律)
    证明同加法卷积,and⁡,or⁡\operatorname{and},\operatorname{or} 同理。
  • (λf)×xorg=f×xor(λg)=λ(f×xorg)(\lambda f)\times_\text{xor} g=f\times_\text{xor} (\lambda g)=\lambda(f\times_\text{xor} g)(与数乘的相容性)
    证明同加法卷积,and⁡,or⁡\operatorname{and},\operatorname{or} 同理。

FFT 的性质

在 FFT 的实现过程中,我们得到了两个很重要的处理方法: 奇偶分段和蝴蝶变换。
现在让我们仔细审视一下 FFT 的变换过程。
分治证明式:

f(wnk)=f0(wn2k)+wnkf1(wn2k)f(w^k_n)=f_0(w^k_\frac{n}{2})+w^k_n f_1(w^k_\frac{n}{2}) f(wnk+n2)=f0(wn2k)−wnkf1(wn2k)f(w^{k+\frac{n}{2}}_n)=f_0(w^k_\frac{n}{2})-w^k_n f_1(w^k_\frac{n}{2})

实际变换过程:

f0(wn2k)f1(wn2k)⇓⇓f0(wn2k)+wnkf1(wn2k)f0(wn2k)−wnkf1(wn2k)\begin{matrix} f_0(w^k_\frac{n}{2}) & f_1(w^k_\frac{n}{2}) \\ \Downarrow & \Downarrow \\ f_0(w^k_\frac{n}{2})+w^k_n f_1(w^k_\frac{n}{2}) & f_0(w^k_\frac{n}{2})-w^k_n f_1(w^k_\frac{n}{2}) \end{matrix}

注意到单次卷积 DFT →\rightarrow 点乘 →\rightarrow IDFT 实际上是一个单次递归过程。
( DFT 时向下递归,点乘时到达底部,IDFT 时向上回溯)
那么 IDFT 就是:

f0(wn2−k)f1(wn2−k)⇓⇓f0(wn2−k)+wn−kf1(wn2−k)f0(wn2−k)−wn−kf1(wn2−k)\begin{matrix} f_0(w^{-k}_\frac{n}{2}) & f_1(w^{-k}_\frac{n}{2}) \\ \Downarrow & \Downarrow \\ f_0(w^{-k}_\frac{n}{2})+w^{-k}_n f_1(w^{-k}_\frac{n}{2}) & f_0(w^{-k}_\frac{n}{2})-w^{-k}_n f_1(w^{-k}_\frac{n}{2}) \end{matrix}

设 F(f)\mathcal{F}(f) 表示对序列 ff 作 DFT 以后的序列, F−1(f)\mathcal{F}^{-1}(f) 表示对序列 ff 作 IDFT 以后的序列。
我们已经知道了以下性质:

F−1(F(f))=F(F−1(f))=f\mathcal{F}^{-1}(\mathcal{F}(f))=\mathcal{F}(\mathcal{F}^{-1}(f))=f F(f×g)k=F(f)kF(g)k\mathcal{F}(f\times g)_k=\mathcal{F}(f)_k \mathcal{F}(g)_k F(f+g)=F(f)+F(g),F(λf)=λF(f)\mathcal{F}(f+ g)=\mathcal{F}(f)+ \mathcal{F}(g),\mathcal{F}(\lambda f)=\lambda \mathcal{F}(f)

FWT的推导过程

我们希望通过与 FFT 类似的方法实现位运算卷积。
设 Fxor(f)\mathcal{F}_\text{xor}(f) 表示 FWT 的异或形式,则其至少满足:

f×xorg=Fxor−1(Fxor(f)⋅Fxor(g))f\times_\text{xor}g=\mathcal{F}^{-1}_\text{xor}(\mathcal{F}_\text{xor}(f)\cdot \mathcal{F}_\text{xor}(g))

其中 ⋅\cdot 表示对应位置相乘。

已知 c=a×xorbc=a\times_\text{xor}b,设三个序列奇偶分段后分别为 c0,c1,a0,a1,b0,b1c_0,c_1,a_0,a_1,b_0,b_1,则显然有

c0=(a0×xorb0)+(a1×xorb1)c_0=(a_0\times_\text{xor}b_0)+(a_1\times_\text{xor}b_1) c1=(a1×xorb0)+(a0×xorb1)c_1=(a_1\times_\text{xor}b_0)+(a_0\times_\text{xor}b_1)

考虑蝴蝶变换,发现

(a0+a1)×xor(b0+b1)=(a0×xorb0)+(a1×xorb1)+(a0×xorb1)+(a1×xorb0)(a_0+a_1)\times_\text{xor}(b_0+b_1)=(a_0\times_\text{xor}b_0)+(a_1\times_\text{xor}b_1)+(a_0\times_\text{xor}b_1)+(a_1\times_\text{xor}b_0) (a0−a1)×xor(b0−b1)=(a0×xorb0)+(a1×xorb1)−(a0×xorb1)−(a1×xorb0)(a_0-a_1)\times_\text{xor}(b_0-b_1)=(a_0\times_\text{xor}b_0)+(a_1\times_\text{xor}b_1)-(a_0\times_\text{xor}b_1)-(a_1\times_\text{xor}b_0)

其中 −- 为向量减法,即 a−b=a+(−1)ba-b=a+(-1)b。

显然,逆变换时只要对如上两式加减消元即可。
于是可得 Fxor\mathcal{F}_\text{xor} 为:

a0a1b0b1⇓⇓⇓⇓a0+a1a0−a1b0+b1b0−b1\begin{matrix} a_0 & a_1 & b_0 & b_1\\ \Downarrow & \Downarrow & \Downarrow & \Downarrow \\ a_0+ a_1 & a_0-a_1 & b_0+ b_1 & b_0-b_1 \end{matrix}

向下递归,计算出卷积

d0=(a0+a1)×xor(b0+b1)d_0=(a_0+a_1)\times_\text{xor}(b_0+b_1) d1=(a0−a1)×xor(b0−b1)d_1=(a_0-a_1)\times_\text{xor}(b_0-b_1)

回溯时,逆变换为

12(d0+d1)12(d0−d1)⇑⇑d0d1\begin{matrix} \dfrac{1}{2}(d_0+d_1) & \dfrac{1}{2}(d_0-d_1) \\ \Uparrow & \Uparrow \\ d_0 & d_1 \end{matrix}

注意到系数 12\dfrac{1}{2} 可以提出来,到最后再除,因此可得到: Fxor−1(f)k=2−nFxor(f)k\mathcal{F}^{-1}_\text{xor}(f)_k=2^{-n}\mathcal{F}_\text{xor}(f)_k
类似的,可以得到 and⁡,or⁡\operatorname{and},\operatorname{or} 的位运算卷积,这里读者自行推导,只给出结果:

a0a1a0−a1a1Fand:⇓⇓Fand−1:⇑⇑a0+a1a1a0a1\begin{matrix} & a_0 & a_1 & & a_0-a_1 & a_1\\ \mathcal{F}_\text{and}: & \Downarrow & \Downarrow & \mathcal{F}^{-1}_\text{and}: & \Uparrow & \Uparrow \\ & a_0+ a_1 & a_1 & & a_0 & a_1 \end{matrix} a0a1a0a1−a0For:⇓⇓For−1:⇑⇑a0a1+a0a0a1\begin{matrix} & a_0 & a_1 & & a_0 & a_1-a_0\\ \mathcal{F}_\text{or}: & \Downarrow & \Downarrow & \mathcal{F}^{-1}_\text{or}: & \Uparrow & \Uparrow \\ & a_0 & a_1+ a_0 & & a_0 & a_1 \end{matrix}

自此,我们已经可以 Θ(nlog⁡n)\Theta(n\log n) 实现单次位运算卷积,即:

f×xorg=Fxor−1(Fxor(f)⋅Fxor(g))f\times_\text{xor}g=\mathcal{F}^{-1}_\text{xor}(\mathcal{F}_\text{xor}(f)\cdot \mathcal{F}_\text{xor}(g))

FWT 的性质

为了方便使用,我们希望 FWT 满足与 FFT 类似的性质。

  • Fxor−1(Fxor(f))=f\mathcal{F}_\text{xor}^{-1}(\mathcal{F}_\text{xor}(f))=f
    证明:在序列长度为 20=12^0=1 时,Fxor(f)=Fxor−1(f)=f\mathcal{F}_\text{xor}(f)=\mathcal{F}^{-1}_\text{xor}(f)=f,性质显然成立
    序列长度 >1>1 时,考虑不做卷积,只有蝴蝶变换,得到 f0f1Fxor:⇓⇓f0+f1f0−f1递归:⇓⇓g0=Fxor−1(Fxor(f0+f1))g1=Fxor−1(Fxor(f0−f1))Fxor−1:⇓⇓12(g0+g1)12(g0−g1)\begin{matrix} & f_0 & f_1 \\ \mathcal{F}_\text{xor}: & \Downarrow & \Downarrow \\ & f_0+f_1 & f_0-f_1 \\ \texttt{递归:} & \Downarrow & \Downarrow \\ & g_0=\mathcal{F}^{-1}_\text{xor}(\mathcal{F}_\text{xor}(f_0+f_1)) & g_1=\mathcal{F}^{-1}_\text{xor}(\mathcal{F}_\text{xor}(f_0-f_1)) \\ \mathcal{F}^{-1}_\text{xor}: & \Downarrow & \Downarrow \\ & \dfrac{1}{2}(g_0+g_1) & \dfrac{1}{2}(g_0-g_1) \end{matrix} 显然 12((f0+f1)+(f0−f1))=12(2f0)=f0\dfrac{1}{2}((f_0+f_1)+(f_0-f_1))=\frac{1}{2}(2f_0)=f_0 12((f0+f1)−(f0−f1))=12(2f1)=f1\dfrac{1}{2}((f_0+f_1)-(f_0-f_1))=\frac{1}{2}(2f_1)=f_1 便可递归证明。
    and⁡,or⁡\operatorname{and},\operatorname{or} 证明类似: Fand:(a0+a1)−a1=a0a1=a1For:a0=a0(a1+a0)−a0=a1\begin{matrix} \mathcal{F}_\text{and}: & (a_0+a_1)-a_1=a_0 & a_1=a_1 \\ \mathcal{F}_\text{or}: & a_0=a_0 & (a_1+a_0)-a_0=a_1 \end{matrix}
  • Fxor(Fxor−1(f))=f,Fxor(f+g)=Fxor(f)+Fxor(g),Fxor(λf)=λFxor(f)\mathcal{F}_\text{xor}(\mathcal{F}^{-1}_\text{xor}(f))=f,\mathcal{F}_\text{xor}(f+ g)=\mathcal{F}_\text{xor}(f)+ \mathcal{F}_\text{xor}(g),\mathcal{F}_{\text{xor}}(\lambda f)=\lambda \mathcal{F}_{\text{xor}}(f)
    同上,此处不再赘述。
  • Fxor(f×xorg)k=Fxor(f)kFxor(g)k\mathcal{F}_\text{xor}(f\times_\text{xor} g)_k=\mathcal{F}_\text{xor}(f)_k \mathcal{F}_\text{xor}(g)_k
    证明:已知 f×xorg=Fxor−1(Fxor(f)⋅Fxor(g))f\times_\text{xor}g=\mathcal{F}^{-1}_\text{xor}(\mathcal{F}_\text{xor}(f)\cdot \mathcal{F}_\text{xor}(g)),则
Fxor(f×xorg)=Fxor(Fxor−1(Fxor(f)⋅Fxor(g)))=Fxor(f)⋅Fxor(g)\mathcal{F}_\text{xor}(f\times_\text{xor}g)=\mathcal{F}_\text{xor}(\mathcal{F}^{-1}_\text{xor}(\mathcal{F}_\text{xor}(f)\cdot \mathcal{F}_\text{xor}(g)))=\mathcal{F}_\text{xor}(f)\cdot \mathcal{F}_\text{xor}(g)

总结与代码

经过严谨的推导,我们得出了 FWT 的实现,
并发现其具有与 FFT 类似的性质,这为我们解题带来了极大的方便。

C++
#include<stdio.h>
#include<string.h>
typedef unsigned char byte;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned int word;
struct READ{
	char c;
	inline READ(){c=getchar();}
	template<typename type>
	inline READ& operator>>(register type& num){
		while('0'>c||c>'9') c=getchar();
		for(num=0;'0'<=c&&c<='9';c=getchar())
			num=num*10+(c-'0');
		return *this;
	}
};//快读快写 
#define mx 17
#define mx_ 16
word root[1<<mx],inv[1<<mx],realid[1<<mx];
const word mod=998244353;
inline ull pow(register ull a,register word b){
	register ull ans=1;
	for(;b;b>>=1){
		if(b&1) (ans*=a)%=mod;
		(a*=a)%=mod;
	}
	return ans;
}//快速幂 
#define loading()			\
	register ull num1=pow(3,(mod-1)>>mx);	\
	register ull num2=pow(num1,mod-2);	\
	register word head,i;	\
	register byte floor;	\
	register ull ni2=pow(2,mod-2);	\
	root[0]=inv[0]=1;	\
	for(i=1;head=i,i<(1<<mx);++i){	\
		root[i]=num1*root[i-1]%mod;	\
		inv[i]=num2*inv[i-1]%mod;	\
		for(floor=0;floor<mx;++floor,head>>=1)	\
			realid[i]=realid[i]<<1|(head&1);	\
	}
//单位根及下标翻转初始化 
#define load()	\
	register ull num1,num2;\
	register word head,i;	\
	register byte floor
//floor 变换区间大小
//head 变换区间头指针 
//i 变换位置 
#define nttfor(size)	\
	for(floor=0;floor<(size);++floor)	\
		for(head=0;head<(1u<<(size));head+=1u<<(floor+1))	\
			for(i=0;i<(1u<<(floor));++i)
//FFT/FWT循环 
#define ntt(num,root)(	\
	num1=(num)[head+i],	\
	num2=(num)[head+i+(1<<floor)],	\
	(num)[head+i]=(num1+((num2*=root[i<<(mx_-floor)])%=mod))%mod,	\
	(num)[head+i+(1u<<floor)]=(num1+mod-num2)%mod)
//FFT蝴蝶变换 
#define _and(num)(	\
	num1=(num)[head+i],	\
	(num)[head+i]=(num1+(num)[head+i+(1u<<floor)])%mod)
//FWT and 蝴蝶变换 
#define and_(num)(	\
	num1=(num)[head+i],	\
	(num)[head+i]=(num1+mod-(num)[head+i+(1u<<floor)])%mod)
//逆FWT and 蝴蝶变换 
#define _or(num)(	\
	num2=(num)[head+i+(1<<floor)],	\
	(num)[head+i+(1u<<floor)]=(num2+(num)[head+i])%mod)
//FWT or 蝴蝶变换 
#define or_(num)(	\
	num2=(num)[head+i+(1<<floor)],	\
	(num)[head+i+(1u<<floor)]=(num2+mod-(num)[head+i])%mod)
//逆FWT and 蝴蝶变换 
#define _xor(num)(	\
		num1=(num)[head+i],	\
		num2=(num)[head+i+(1<<floor)],	\
		(num)[head+i]=(num1+num2)%mod,	\
		(num)[head+i+(1<<floor)]=(num1+mod-num2)%mod)
//FWT xor 蝴蝶变换 
#define id(size,i) (realid[i]>>(mx-(size)))
//下标二进制翻转的编号 
#define FOR(size) for(i=0;i<1u<<(size);i++)
word eax[1<<mx],ebx[1<<mx],ecx[1<<mx],edx[1<<mx],eex[1<<mx],efx[1<<mx];
int main(){
	byte size;
	register READ cin;
	cin>>size;
	loading();
	FOR(size) cin>>eax[id(size,i)];
	FOR(size) cin>>ebx[id(size,i)];
	memcpy(ecx,eax,4u<<size);
	memcpy(edx,ebx,4u<<size);
	memcpy(eex,eax,4u<<size);
	memcpy(efx,ebx,4u<<size);
	nttfor(size){
		_or(eax),_or(ebx);
		_and(ecx),_and(edx);
	}
	FOR(size){
		head=id(size,i);
		root[head]=(ull)(eax[i])*ebx[i]%mod;
		inv[head]=(ull)(ecx[i])*edx[i]%mod;
	}
	nttfor(size) or_(root),and_(inv);
	FOR(size) printf("%u ",root[i]);
	putchar('\n');
	FOR(size) printf("%u ",inv[i]);
	putchar('\n');
	nttfor(size) _xor(eex),_xor(efx);
	FOR(size) root[id(size,i)]=(ull)(eex[i])*efx[i]%mod;
	nttfor(size) _xor(root);
    num1=pow(ni2,size);
	FOR(size) printf("%u ",num1*root[i]%mod);
	// $\mathcal{F}_\text{xor}^{-1}(f)_k=2^{-n}\mathcal{F}_\text{xor}(F)_k$
	return 0;
}