Luogu4717 【模板】快速沃尔什变换(FWT)

时间:2024-04-25 11:04:38

  https://www.cnblogs.com/RabbitHu/p/9182047.html

  完全没有学证明的欲望因为这个实在太好写了而且FFT就算学过也忘得差不多了只会写板子

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N (1<<17)
#define P 998244353
#define inv2 499122177
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,a[N],b[N],f[N],g[N];
void OR(int *a,int n,int op)
{
for (int i=;i<=n;i<<=)
for (int j=;j<n;j+=i)
for (int k=j;k<j+(i>>);k++)
{
int x=a[k],y=a[k+(i>>)];
if (op==) a[k]=x,a[k+(i>>)]=(x+y)%P;
else a[k]=x,a[k+(i>>)]=(y-x+P)%P;
}
}
void AND(int *a,int n,int op)
{
for (int i=;i<=n;i<<=)
for (int j=;j<n;j+=i)
for (int k=j;k<j+(i>>);k++)
{
int x=a[k],y=a[k+(i>>)];
if (op==) a[k]=(x+y)%P,a[k+(i>>)]=y;
else a[k]=(x-y+P)%P,a[k+(i>>)]=y;
}
}
void XOR(int *a,int n,int op)
{
for (int i=;i<=n;i<<=)
for (int j=;j<n;j+=i)
for (int k=j;k<j+(i>>);k++)
{
int x=a[k],y=a[k+(i>>)];
a[k]=(x+y)%P,a[k+(i>>)]=(x-y+P)%P;
if (op==) a[k]=1ll*a[k]*inv2%P,a[k+(i>>)]=1ll*a[k+(i>>)]*inv2%P;
}
}
void FWT(int *a,int *b,int n,int op)
{
if (op==) OR(a,n,),OR(b,n,);
else if (op==) AND(a,n,),AND(b,n,);
else if (op==) XOR(a,n,),XOR(b,n,);
for (int i=;i<n;i++) a[i]=1ll*a[i]*b[i]%P;
if (op==) OR(a,n,);
else if (op==) AND(a,n,);
else if (op==) XOR(a,n,);
for (int i=;i<n;i++) printf("%d ",f[i]);cout<<endl;
}
int main()
{
freopen("FWT.in","r",stdin);
freopen("FWT.out","w",stdout);
n=<<read();
for (int i=;i<n;i++) a[i]=read();
for (int i=;i<n;i++) b[i]=read();
memcpy(f,a,sizeof(f));memcpy(g,b,sizeof(g));
FWT(f,g,n,);
memcpy(f,a,sizeof(f));memcpy(g,b,sizeof(g));
FWT(f,g,n,);
memcpy(f,a,sizeof(f));memcpy(g,b,sizeof(g));
FWT(f,g,n,);
return ;
}