FWT模板
Posted goto_1600
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FWT模板相关的知识,希望对你有一定的参考价值。
//#pragma GCC target("avx")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
// created by myq
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
#define x first
#define y second
typedef pair<int,int> pii;
const int N = 1<<17;
const int mod=998244353;
inline int read()
{
int res=0;
int f=1;
char c=getchar();
while(c>'9' ||c<'0')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
res=(res<<3)+(res<<1)+c-'0';
}
return res;
}
#define int long long
int n;
int len;
int a[N];
int b[N];
int ca[N];
int cb[N];
int qmi(int a,int b){
int res=1;
while(b){
if(b&1) res=res*a%mod;
b>>=1;
a=a*a%mod;
}
return res;
}
int inv2=qmi(2,mod-2);
void fwt_xor(int f[],int inv=1)//^
{
for(int mid=1;mid<len;mid*=2){
for(int i=0;i<len;i+=2*mid){
for(int k=0;k<mid;k++){
int j=i+mid;
int x=f[i+k];
int y=f[j+k];
f[i+k]=((x+y)%mod+mod)%mod;
f[j+k]=((x-y)%mod+mod)%mod;
if(inv==-1)
{
f[i+k]=f[i+k]*inv2%mod;
f[j+k]=f[j+k]*inv2%mod;
}
}
}
}
}
void fwt_and(int f[],int x=1){//&
for(int mid=1;mid<len;mid*=2){
for(int i=0;i<len;i+=2*mid){
for(int k=0;k<mid;k++){
int j=i+mid;
f[i+k]=(f[i+k]+x*f[j+k]+mod)%mod;
}
}
}
}
void fwt_or(int f[],int x=1){//|
for(int mid=1;mid<len;mid*=2){
for(int i=0;i<len;i+=2*mid){
for(int k=0;k<mid;k++){
int j=i+mid;
f[j+k]=(f[j+k]+f[i+k]*x+mod)%mod;
}
}
}
}
signed main()
{
cin>>n;
len=(1<<n);
for(int i=0;i<1<<n;i++)
cin>>a[i];
for(int i=0;i<1<<n;i++)
cin>>b[i];
memcpy(ca,a,sizeof a);
memcpy(cb,b,sizeof b);
fwt_or(a);
fwt_or(b);
for(int i=0;i<len;i++)
a[i]=a[i]*b[i]%mod;
fwt_or(a,-1);
for(int i=0;i<len;i++) cout<<a[i]<<" ";
cout<<endl;
memcpy(a,ca,sizeof a);
memcpy(b,cb,sizeof b);
fwt_and(a);
fwt_and(b);
for(int i=0;i<len;i++)
a[i]=a[i]*b[i]%mod;
fwt_and(a,-1);
for(int i=0;i<len;i++) cout<<a[i]<<" ";
cout<<endl;
memcpy(a,ca,sizeof a);
memcpy(b,cb,sizeof b);
fwt_xor(a);
fwt_xor(b);
for(int i=0;i<len;i++)
a[i]=a[i]*b[i]%mod;
fwt_xor(a,-1);
for(int i=0;i<len;i++) cout<<a[i]<<" ";
cout<<endl;
return 0;
}
/**
* In every life we have some trouble
* When you worry you make it double
* Don't worry,be happy.
**/
以上是关于FWT模板的主要内容,如果未能解决你的问题,请参考以下文章