引入

沃尔什变换的数学定义

沃尔什转换矩阵

沃尔什转换矩阵为一个方阵,其阶数为 22 的幂。2k2^k 的沃尔什矩阵可用迭代方式产生。
起始点 k=1k=1,定义 212^1 阶沃尔什转换矩阵 W2=(1111)W_2=\begin{pmatrix}1&1\\1&-1\end{pmatrix}。假设已经有 2k2^k 阶沃尔什转换矩阵 W2kW_{2^k},定义 V2k+1=(W2kW2kW2kW2k)V_{2^{k+1}}=\begin{pmatrix}W_{2^k}&W_{2^k}\\W_{2^k}&-W_{2^k}\end{pmatrix},再将 V2k+1V_{2^{k+1}} 的列重新排列得到 W2k+1W_{2^{k+1}}
在不同的应用领域,对 V2k+1V_{2^{k+1}} 列的排序时,要按照不同的顺序排序。

Walsh Ordering Paley Ordering Hadamard Ordering
常用于信号处理 较常用于控制工程 较常用于数学

正变换与逆变换

沃尔什变换的转换式为 Fi=j=0n1fjWijF_i=\sum\limits_{j=0}^{n-1}f_jW_{ij}WijW_{ij} 是沃尔什变换矩阵的第 ii 行第 jj 列的元素。其逆变换为 fi=1nj=0n1FjWijf_i=\frac{1}{n}\sum\limits_{j=0}^{n-1}F_jW_{ij}

沃尔什变换的在通信中的应用

沃尔什变换是在频谱分析上作为离散傅立叶变换的替代方案的一种方法。
在频谱分析上最常用的一种方法是使用傅立叶变换。然而,即使已经有 FFT\text{FFT} 来实现傅立叶变换,仍然具有一些实现上的缺点。
在傅立叶变换中,向量必须乘上复数矩阵加以处理,且复数的实部和虚部大多数情况下都为浮点数,因此计算量和误差会比较大。
在沃尔什变换中,向量需要乘上的矩阵是一个整数的矩阵,而且这些矩阵的元素只可能是 111–1,这使得我们不需要作浮点数的乘法运算。更进一步,只需要使用加法来实现沃尔什变换,这使的沃尔什变换在运算复杂度上远小于傅立叶变换。
使用傅立叶变换相当于把信号拆解成在不同频率的正弦函数与余弦函数的分量,而使用沃尔什转换相当于把信号拆解成在许多不同震荡频率的方波上。因此,除非所要分析的信号拥有类似方波组合的特性,使用沃尔什变换作频谱分析的效果会比使用离散傅立叶变换分析的效果要差,这是降低运算复杂度所要付出的代价。
CDMA\text{CDMA} 就应用了沃尔什变化做多工。

沃尔什变换在信息学竞赛中的应用

在信息学竞赛中,沃尔什变换用于解决对下标进行位运算卷积的问题。快速沃尔什变换,简称 FWT\mathrm{FWT},能够在 O(nlogn)O(n\log n) 的时间内完成沃尔什变换及其逆变换。

一些约定

  • \wedge 为与运算符,\vee 为或运算符,\oplus 为异或运算符。
  • 一般用大写字母 AA 表示一个多项式,nn 为多项式的长度。
  • 对于所有讨论的多项式,其长度 nn 都为 22 的幂次。
  • AA 为长度为 nnn1n-1 次多项式,其系数表达式为 (a0,a1,,an1)(a_0,a_1,\cdots,a_{n-1})。定义 A0A_0AA 的前 n2\frac{n}{2} 项,A1A_1AA 的后 n2\frac{n}{2} 项。
  • A+BA+B 表示将多项式 A,BA,B 对应的系数相加,即 ci=ai+bic_i=a_i+b_i
  • ABA-B 表示将多项式 A,BA,B 对应的系数相减,即 ci=aibic_i=a_i-b_i
  • A×BA\times B 表示将多项式 A,BA,B 对应的系数相乘,即 ci=aibic_i=a_ib_i
  • FWT(A)\text{FWT}(A) 表示将多项式 AA 进行快速沃尔什变换,其结果为一个长度与 AA 相同的多项式。
  • IFWT(A)\text{IFWT}(A) 表示将多项式 AA 进行逆快速沃尔什变换,显然有 IFWT(FWT(A))=A\text{IFWT}(\text{FWT}(A))=A
  • [A,B][A,B] 表示多项式的拼接。AA 的系数表达式为 (a0,a1,,an1)(a_0,a_1,\cdots,a_{n_1})BB 的系数表达式为 (b0,b1,,bn2)(b_0,b_1,\cdots,b_{n_2}),那么 [A,B][A,B] 的系数表达式为 (a0,,an1,b0,,bn2)(a_0,\cdots,a_{n_1},b_0,\cdots,b_{n_2})
  • C=ABC=A\wedge B 表示对下标进行与运算卷积,ci=jk=iajbkc_i=\sum\limits_{j\wedge k=i}a_jb_k
  • C=ABC=A\vee B 表示对下标进行或运算卷积,ci=jk=iajbkc_i=\sum\limits_{j\vee k=i}a_jb_k
  • C=ABC=A\oplus B 表示对下标进行异或运算卷积,ci=jk=iajbkc_i=\sum\limits_{j\oplus k=i}a_jb_k

引理

引理 1 [A+B,C+D]=[A,C]+[B,D][A+B,C+D]=[A,C]+[B,D]
略证 将多项式用系数表达式表示得证。
引理 2 [A×B,C×D]=[A,C]×[B,D][A\times B,C\times D]=[A,C]\times[B,D]
略证 将多项式用系数表达式表示得证。
**引理 3 ** (A+B)0=A0+B0(A+B)_0=A_0+B_0(A+B)1=A1+B1(A+B)_1=A_1+B_1
略证 将多项式化为系数表达式得证。
引理 4 [(AB)0,(AB)1]=[A0B0+A0B1+A1B0,A1B1][(A\wedge B)_0,(A\wedge B)_1]=[A_0\wedge B_0+A_0\wedge B_1+A_1\wedge B_0,A_1\wedge B_1]
略证 A0A_0 的各个下标范围是 0n210\sim \frac{n}{2}-1,而 A1A_1 的下标范围是 n2n1\frac{n}{2}\sim n-1A1A_1 中下标的最高位是 11,而 A0A_0 中下标的最高位为 00。因此卷积结果的前半部分为三项之和:A0B0+A0B1+A1B0A_0\wedge B_0+A_0\wedge B_1+A_1\wedge B_0,每一项的下标范围都在 0n210\sim \frac{n}{2}-1。后半部分为 A1B1A_1\wedge B_1,下标范围在 n2n1\frac{n}{2}\sim n-1 之间。
引理 5 [(AB)0,(AB)1]=[A0B0,A1B0+A0B1+A1B1][(A\vee B)_0,(A\vee B)_1]=[A_0\vee B_0,A_1\vee B_0+A_0\vee B_1+A_1\vee B_1]
略证 A0A_0 的各个下标范围是 0n210\sim \frac{n}{2}-1,而 A1A_1 的下标范围是 n2n1\frac{n}{2}\sim n-1A1A_1 中下标的最高位是 11,而 A0A_0 中下标的最高位为 00。因此卷积结果的前半部分为 A0B0A_0\vee B_0,下标范围在 0n210\sim\frac{n}{2}-1 之间。后半部分为三项之和:A0B0+A0B1+A1B0A_0\vee B_0+A_0\vee B_1+A_1\vee B_0,每一项的下标范围都在 n2n1\frac{n}{2}\sim n-1 之间。
引理 6 [(AB)0,(AB)1]=[A0B0+A1B1,A1B0+A0B1][(A\oplus B)_0,(A\oplus B)_1]=[A_0\oplus B_0+A_1\oplus B_1,A_1\oplus B_0+A_0\oplus B_1]
证明 A0A_0 的各个下标范围是 0n210\sim \frac{n}{2}-1,而 A1A_1 的下标范围是 n2n1\frac{n}{2}\sim n-1A1A_1 中下标的最高位是 11,而 A0A_0 中下标的最高位为 00。因此卷积结果的前半部分为 A0B0A_0\oplus B_0A1B1A_1\oplus B_1 之和,两项的下标范围均在 0n210\sim\frac{n}{2}-1 之间。后半部分为 A0B1A_0\oplus B_1A1B0A_1\oplus B_0 之和,两项的下标范围都在 n2n1\frac{n}{2}\sim n-1 之间。

三种卷积

基于以上的约定,对于两个长度为 nnn1n-1 次多项式 A,BA,BFWT\text{FWT} 可以解决三种对下标进行位运算卷积的问题:ABA\wedge BABA\vee BABA\oplus B
类似 FFT\text{FFT},我们需要定义 FWT\text{FWT},先正向得到 FWT(A)\text{FWT}(A)FWT(B)\text{FWT}(B),然后能够在线性的时间内得到 FWT(C)\text{FWT}(C),最后进行逆变换即可得到 CC
接下来依次介绍求解三种卷积的方法,对于不同的卷积,FWT\text{FWT} 的定义形式也有些许不同。

与卷积 C=ABC=A\wedge B

定义

定义 FWT(A)={An=1[FWT(A0+A1),FWT(A1)]n>1\text{FWT}(A)=\begin{cases}A&n=1\\\left[\text{FWT}(A_0+A_1),\text{FWT}(A_1)\right]&n>1\end{cases}
同时不加证明地给出逆变换 IFWT(A)={An=1[IFWT(A0A1),IFWT(A1)]n>1\mathrm{IFWT}(A)=\begin{cases}A&n=1\\\left[\mathrm{IFWT}(A_0-A_1),\mathrm{IFWT}(A_1)\right]&n>1\end{cases}

定理

定理 1.1 FWT(A+B)=FWT(A)+FWT(B)\mathrm{FWT}(A+B)=\mathrm{FWT}(A)+\mathrm{FWT}(B)
证明 用数学归纳法证明。
n=1n=1 时,FWT(A+B)=A+B=FWT(A)+FWT(B)\mathrm{FWT}(A+B)=A+B=\mathrm{FWT}(A)+\mathrm{FWT}(B),显然成立。
假设当 n=k2n=\frac{k}{2} 时等式成立,其中 kk22 的正整数幂。
n=kn=k 时,
 FWT(A+B)=[FWT((A+B)0+(A+B)1),FWT((A+B)1)]=[FWT(A0+A1+B0+B1),FWT(A1+B1)]=[FWT(A0)+FWT(A1)+FWT(B0)+FWT(B1),FWT(A1)+FWT(B1)]=[FWT(A0)+FWT(A1),FWT(A1)]+[FWT(B0)+FWT(B1),FWT(B1)]=FWT(A)+FWT(B)\begin{aligned}&\quad\ \mathrm{FWT}(A+B)\\&=[ \mathrm{FWT}((A+B)_0+(A+B)_1),\mathrm{FWT}((A+B)_1)]\\&=[\mathrm{FWT}(A_0+A_1+B_0+B_1),\mathrm{FWT}(A_1+B_1)]\\&=[\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1)+\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1),\mathrm{FWT}(A_1)+\mathrm{FWT}(B_1)]\\&=[\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1),\mathrm{FWT(A_1)}]+[\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1),\mathrm{FWT}(B_1)]\\&=\mathrm{FWT}(A)+\mathrm{FWT}(B)\end{aligned}
证毕。
定理 1.2 FWT(AB)=FWT(A)×FWT(B)\mathrm{FWT}(A\wedge B)=\mathrm{FWT}(A)\times\mathrm{FWT}(B)
证明 用数学归纳法证明。
n=1n=1 时,FWTAB=A×B=FWT(A)×FWT(B)\mathrm{FWT}{A\wedge B}=A\times B=\mathrm{FWT}(A)\times \mathrm{FWT}(B),等式成立。
假设当 n=k2n=\frac{k}{2} 时等式成立,其中 kk22 的正整数次幂。
n=kn=k 时,
 FWT(AB)=FWT([(AB)0,(AB)1])=FWT([A0B0+A1B0+A0B1,A1B1])=[FWT(A0B0+A1B0+A0B1+A1B1),FWT(A1B1)]=[FWT(A0B0)+FWT(A1B0)+FWT(A0B1)+FWT(A1B1),FWT(A1B1)]=[FWT(A0)×FWT(B0)+FWT(A1)×FWT(B0)+FWT(A0)×FWT(B1) +FWT(A1)×FWT(B1),FWT(A1)×FWT(B1)]=[(FWT(A0)+FWT(A1))×(FWT(B0)+FWT(B1)),FWT(A1)×FWT(B1)]=[FWT(A0+A1)×FWT(B0+B1),FWT(A1)×FWT(B1)]=[FWT(A0+A1),FWT(A1)]×[FWT(B0+B1),FWT(B1)]\begin{aligned}&\quad\ \mathrm{FWT}(A\wedge B)\\&=\mathrm{FWT}([(A\wedge B)_0,(A\wedge B)_1])\\&=\mathrm{FWT}([A_0\wedge B_0+A_1\wedge B_0+A_0\wedge B_1,A_1\wedge B_1])\\&=[\mathrm{FWT}(A_0\wedge B_0+A_1\wedge B_0+A_0\wedge B_1+A_1\wedge B_1),\mathrm{FWT}(A_1\wedge B_1)]\\&=[\mathrm{FWT}(A_0\wedge B_0)+\mathrm{FWT}(A_1\wedge B_0)+\mathrm{FWT}(A_0\wedge B_1)+\mathrm{FWT}(A_1\wedge B_1),\mathrm{FWT}(A_1\wedge B_1)]\\&=[\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_0)+\mathrm{FWT}(A_1)\times \mathrm{FWT}(B_0)+\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_1)\\&\ \quad+\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_1),\mathrm{FWT}(A_1)\times \mathrm{FWT}(B_1)]\\&=[(\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1))\times(\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1)),\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_1)]\\&=[\mathrm{FWT}(A_0+A_1)\times\mathrm{FWT}(B_0+B_1),\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_1)]\\&=[\mathrm{FWT}(A_0+A_1),\mathrm{FWT}(A_1)]\times[\mathrm{FWT}(B_0+B_1),\mathrm{FWT}(B_1)]\end{aligned}
证毕。

代码

基于定理1.2C=IFWT(FWT(AB))=IFWT(FWT(A)×FWT(B))C=\mathrm{IFWT}(\mathrm{FWT}(A\wedge B))=\mathrm{IFWT}(\mathrm{FWT}(A)\times\mathrm{FWT}(B))
正变换:FWT(A)=[FWT(A0+A1),FWT(A1)]\text{FWT}(A)=[\text{FWT}(A_0+A_1),\text{FWT}(A_1)]
逆变换:IFWT(A)=[IFWT(A0A1),IFWT(A1)]\mathrm{IFWT}(A)=[\mathrm{IFWT}(A_0-A_1),\mathrm{IFWT}(A_1)]
可用分治法解决。

1
2
3
4
5
6
7
8
9
10
11
void FWT_AND(ll *x,short opt){
for(register int i=1;i<n;i<<=1){
int step=i<<1;
for(register int j=0;j<n;j+=step){
for(register int k=0;k<i;k++){
x[j+k]+=x[j+k+i]*opt;
x[j+k]=(x[j+k]%mod+mod)%mod;
}
}
}
}

或卷积 C=ABC=A\vee B

定义

定义 FWT(A)={An=1[FWT(A0),FWT(A0+A1)]n>1\mathrm{FWT}(A)=\begin{cases}A&n=1\\\left[\mathrm{FWT}(A_0),\mathrm{FWT}(A_0+A_1)\right]&n>1\end{cases}
同时不加证明地给出逆变换 IFWT(A)={An=1[IFWT(A0),IFWT(A1A0)]n>1\mathrm{IFWT}(A)=\begin{cases}A&n=1\\\left[\mathrm{IFWT}(A_0),\mathrm{IFWT}(A_1-A_0)\right]&n>1\end{cases}

定理

定理 2.1 FWT(A+B)=FWT(A)+FWT(B)\mathrm{FWT}(A+B)=\mathrm{FWT}(A)+\mathrm{FWT}(B)
证明 用数学归纳法证明。
n=1n=1 时,FWT(A+B)=A+B=FWT(A)+FWT(B)\mathrm{FWT}(A+B)=A+B=\mathrm{FWT}(A)+\mathrm{FWT}(B),等式成立。
假设当 n=k2n=\frac{k}{2} 时,等式成立。其中,kk22 的正整数幂。
n=kn=k 时,
 FWT(A+B)=[FWT(A0+B0),FWT(A0+B0+A1+B1)]=[FWT(A0)+FWT(B0),FWT(A0)+FWT(B0)+FWT(A1)+FWT(B1)]=[FWT(A0),FWT(A0)+FWT(A1)]+[FWT(B0),FWT(B0)+FWT(B1)]=FWT(A)+FWT(B)\begin{aligned}&\quad\ \mathrm{FWT}(A+B)\\&=[\mathrm{FWT}(A_0+B_0),\mathrm{FWT}(A_0+B_0+A_1+B_1)]\\&=[\mathrm{FWT}(A_0)+\mathrm{FWT}(B_0),\mathrm{FWT}(A_0)+\mathrm{FWT}(B_0)+\mathrm{FWT}(A_1)+\mathrm{FWT}(B_1)]\\&=[\mathrm{FWT}(A_0),\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1)]+[\mathrm{FWT}(B_0),\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1)]\\&=\mathrm{FWT}(A)+\mathrm{FWT}(B)\end{aligned}
证毕。
定理 2.2 FWT(AB)=FWT(A)×FWT(B)\mathrm{FWT}(A\vee B)=\mathrm{FWT}(A)\times \mathrm{FWT}(B)
证明 用数学归纳法证明。
n=1n=1 时,FWT(AB)=A×B=FWT(A)×FWT(B)\mathrm{FWT}(A\vee B)=A\times B=\mathrm{FWT}(A)\times\mathrm{FWT}(B),等式成立。
假设当 n=k2n=\frac{k}{2} 时,等式成立。
n=kn=k 时,
 FWT(AB)=FWT([(AB)0,(AB)1])=FWT([A0B0,A0B1+A1B0+A1B1])=[FWT(A0B0),FWT(A0B0+A0B1+A1B0+A1B1)]=[FWT(A0B0),FWT(A0B0)+FWT(A0B1)+FWT(A1B0)+FWT(A1B1)]=[FWT(A0)×FWT(B0),FWT(A0)×FWT(B0)+FWT(A0)×FWT(B1) +FWT(A1)×FWT(B0)+FWT(A1)×FWT(B1)]=[FWT(A0)×FWT(B0),(FWT(A0)+FWT(A1))×(FWT(B0)+FWT(B1))]=[FWT(A0),FWT(A0+A1)]×[FWT(B0),FWT(B0+B1)]=FWT(A)×FWT(B)\begin{aligned}&\quad\ \mathrm{FWT}(A\vee B)\\&=\mathrm{FWT}([(A\vee B)_0,(A\vee B)_1])\\&=\mathrm{FWT}([A_0\vee B_0,A_0\vee B_1+A_1\vee B_0+A_1\vee B_1])\\&=[\mathrm{FWT}(A_0\vee B_0),\mathrm{FWT}(A_0\vee B_0+A_0\vee B_1+A_1\vee B_0+A_1\vee B_1)]\\&=[\mathrm{FWT}(A_0\vee B_0),\mathrm{FWT}(A_0\vee B_0)+\mathrm{FWT}(A_0\vee B_1)+\mathrm{FWT}(A_1\vee B_0)+\mathrm{FWT}(A_1\vee B_1)]\\&=[\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_0),\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_0)+\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_1)\\&\quad\ +\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_0)+\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_1)]\\&=[\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_0),(\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1))\times(\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1))]\\&=[\mathrm{FWT}(A_0),\mathrm{FWT}(A_0+A_1)]\times[\mathrm{FWT}(B_0),\mathrm{FWT}(B_0+B_1)]\\&=\mathrm{FWT}(A)\times\mathrm{FWT}(B)\end{aligned}

代码

基于定理 2.2C=IFWT(FWT(AB))=IFWT(FWT(A)×FWT(B))C=\mathrm{IFWT}(\mathrm{FWT}(A\vee B))=\mathrm{IFWT}(\mathrm{FWT}(A)\times\mathrm{FWT}(B))
正变换:FWT(A)=[FWT(A0),FWT(A0+A1)]\mathrm{FWT}(A)=[\mathrm{FWT}(A_0),\mathrm{FWT}(A_0+A_1)]
逆变换:IFWT(A)=[IFWT(A0),FWT(A1A0)]\mathrm{IFWT}(A)=[\mathrm{IFWT}(A_0),\mathrm{FWT}(A_1-A_0)]
可用分治法解决。

1
2
3
4
5
6
7
8
9
10
11
void FWT_OR(ll *x,short opt){
for(register int i=1;i<n;i<<=1){
int step=i<<1;
for(register int j=0;j<n;j+=step){
for(register int k=0;k<i;k++){
x[j+k+i]+=x[j+k]*opt;
x[j+k+i]=(x[j+k+i]%mod+mod)%mod;
}
}
}
}

异或卷积 C=ABC=A\oplus B

定义

定义 FWT(A)={An=1[FWT(A0+A1),FWT(A0A1)]n>1\mathrm{FWT}(A)=\begin{cases}A&n=1\\\left[\mathrm{FWT}(A_0+A_1),\mathrm{FWT}(A_0-A_1)\right]&n>1\end{cases}
同时不加证明地给出逆变换 IFWT(A)={An=1[IFWT(A0+A1)2,IFWT(A0A1)2]n>1\mathrm{IFWT}(A)=\begin{cases}A&n=1\\\left[\frac{\mathrm{IFWT}(A_0+A_1)}{2},\frac{\mathrm{IFWT}(A_0-A_1)}{2}\right]&n>1\end{cases}

定理

定理 3.1 FWT(A+B)=FWT(A)+FWT(B)\mathrm{FWT}(A+B)=\mathrm{FWT}(A)+\mathrm{FWT}(B)
证明 用数学归纳法证明。
n=1n=1 时,FWT(A+B)=A+B=FWT(A)+FWT(B)\mathrm{FWT}(A+B)=A+B=\mathrm{FWT}(A)+\mathrm{FWT}(B),等式成立。
假设当 n=k2n=\frac{k}{2} 时,等式成立。其中,kk22 的正整数幂。
n=kn=k 时,
 FWT(A+B)=[FWT((A+B)0+(A+B)1),FWT((A+B)0(A+B)1)]=[FWT(A0)+FWT(A1)+FWT(B0)+FWT(B1),FWT(A0)FWT(A1)+FWT(B0)FWT(B1)]=[FWT(A0)+FWT(A1),FWT(A0)FWT(A1)]+[FWT(B0)+FWT(B1),FWT(B0)FWT(B1)]=FWT(A)+FWT(B)\begin{aligned}&\quad\ \mathrm{FWT}(A+B)\\&=[\mathrm{FWT}((A+B)_0+(A+B)_1),\mathrm{FWT}((A+B)_0-(A+B)_1)]\\&=[\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1)+\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1),\mathrm{FWT}(A_0)-\mathrm{FWT}(A_1)+\mathrm{FWT}(B_0)-\mathrm{FWT}(B_1)]\\&=[\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1),\mathrm{FWT}(A_0)-\mathrm{FWT}(A_1)]+[\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1),\mathrm{FWT}(B_0)-\mathrm{FWT}(B_1)]\\&=\mathrm{FWT}(A)+\mathrm{FWT}(B)\end{aligned}
证毕。
定理 3.2 FWT(AB)=FWT(A)×FWT(B)\mathrm{FWT}(A\oplus B)=\mathrm{FWT}(A)\times\mathrm{FWT}(B)
证明 用数学归纳法证明。
n=1n=1 时,FWT(AB)=A×B=FWT(A)×FWT(B)\mathrm{FWT}(A\oplus B)=A\times B=\mathrm{FWT}(A)\times \mathrm{FWT}(B),等式成立。
假设当 n=k2n=\frac{k}{2} 时等式成立。其中,kk22 的正整数幂。
n=kn=k 时,
 FWT(AB)=FWT([(AB)0,(AB)1])=FWT([A0B0+A1B1,A0B1+A1B0])=[FWT(A0B0+A1B1+A0B1+A1B0+),FWT(A0B0+A1B1A0B1A1B0)]=[FWT(A0B0)+FWT(A1B1)+FWT(A0B1)+FWT(A1B0), FWT(A0B0)+FWT(A1B1)FWT(A0B1)FWT(A1B0)]=[FWT(A0)×FWT(B0)+FWT(A1)×FWT(B1)+FWT(A0)×FWT(B1)+FWT(A1)×FWT(B0), FWT(A0)×FWT(B0)+FWT(A1)×FWT(B1)FWT(A0)×FWT(B1)FWT(A1)×FWT(B0)]=[(FWT(A0)+FWT(A1))×(FWT(B0)+FWT(B1)),(FWT(A0)FWT(A1))×(FWT(B0)FWT(B1))]=[FWT(A0+A1)×FWT(B0+B1),FWT(A0A1)×FWT(B0B1)]=[FWT(A0+A1)FWT(A0A1)]×[FWT(B0+B1)FWT(B0B1)]=FWT(A)×FWT(B)\begin{aligned}&\quad\ \mathrm{FWT}(A\oplus B)\\&=\mathrm{FWT}([(A\oplus B)_0,(A\oplus B)_1])\\&=\mathrm{FWT}([A_0\oplus B_0+A_1\oplus B_1,A_0\oplus B_1+A_1\oplus B_0])\\&=[\mathrm{FWT}(A_0\oplus B_0+A_1\oplus B_1+A_0\oplus B_1+A_1\oplus B_0+),\mathrm{FWT}(A_0\oplus B_0+A_1\oplus B_1-A_0\oplus B_1-A_1\oplus B_0)]\\&=[\mathrm{FWT}(A_0\oplus B_0)+\mathrm{FWT}(A_1\oplus B_1)+\mathrm{FWT}(A_0\oplus B_1)+\mathrm{FWT}(A_1\oplus B_0),\\&\ \quad\mathrm{FWT}(A_0\oplus B_0)+\mathrm{FWT}(A_1\oplus B_1)-\mathrm{FWT}(A_0\oplus B_1)-\mathrm{FWT}(A_1\oplus B_0)]\\&=[\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_0)+\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_1)+\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_1)+\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_0),\\&\ \quad\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_0)+\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_1)-\mathrm{FWT}(A_0)\times\mathrm{FWT}(B_1)-\mathrm{FWT}(A_1)\times\mathrm{FWT}(B_0)]\\&=[(\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1))\times(\mathrm{FWT}(B_0)+\mathrm{FWT}(B_1)),(\mathrm{FWT}(A_0)-\mathrm{FWT}(A_1))\times(\mathrm{FWT}(B_0)-\mathrm{FWT}(B_1))]\\&=[\mathrm{FWT}(A_0+A_1)\times\mathrm{FWT}(B_0+B_1),\mathrm{FWT}(A_0-A_1)\times\mathrm{FWT}(B_0-B_1)]\\&=[\mathrm{FWT}(A_0+A_1)-\mathrm{FWT}(A_0-A_1)]\times[\mathrm{FWT}(B_0+B_1)-\mathrm{FWT}(B_0-B_1)]\\&=\mathrm{FWT}(A)\times\mathrm{FWT}(B)\end{aligned}
证毕。

代码

基于定理 3.2C=IFWT(FWT(AB))=IFWT(FWT(A)×FWT(B))C=\mathrm{IFWT}(\mathrm{FWT}(A\oplus B))=\mathrm{IFWT}(\mathrm{FWT}(A)\times\mathrm{FWT}(B))
正变换:FWT(A)=[FWT(A0)+FWT(A1),FWT(A0)FWT(A1)]\mathrm{FWT}(A)=[\mathrm{FWT}(A_0)+\mathrm{FWT}(A_1),\mathrm{FWT}(A_0)-\mathrm{FWT}(A_1)]
逆变换:IFWT(A)=[IFWT(A0)+IFWT(A1)2,IFWT(A0)IFWT(A1)2]\mathrm{IFWT}(A)=\left[\frac{\mathrm{IFWT}(A_0)+\mathrm{IFWT}(A_1)}{2},\frac{\mathrm{IFWT}(A_0)-\mathrm{IFWT}(A_1)}{2}\right]
可用分治法解决。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void FWT_XOR(ll *x,short opt){
for(register int i=1;i<n;i<<=1){
int step=i<<1;
for(register int j=0;j<n;j+=step){
for(register int k=0;k<i;k++){
const ll u=x[j+k],v=x[j+k+i];
x[j+k]=u+v;
x[j+k+i]=u-v;
x[j+k]=(x[j+k]%mod+mod)%mod;
x[j+k+i]=(x[j+k+i]%mod+mod)%mod;
if(opt==-1){
x[j+k]=x[j+k]*inv2%mod;
x[j+k+i]=x[j+k+i]*inv2%mod;
}
}
}
}
}