F F T : FFT: FFT: C k = ∑ i + j = k A i ∗ B j C_k=\sum_{i+j=k}A_i*B_j Ck=∑i+j=kAi∗Bj
F W T : FWT: FWT: C k = ∑ i ⊕ j = k A i ∗ B j C_k=\sum_{i \oplus j=k}A_i*B_j Ck=∑i⊕j=kAi∗Bj
#include
using namespace std;
const int N = 1 << 17, P = 998244353, inv_2 = 499122177;
inline int add(int a, int b){
return a + b >= P ? a + b - P : a + b;}
inline int sub(int a, int b){
return a - b < 0 ? a - b + P : a - b;}
inline int mul(int a, int b){
return 1ll * a * b - 1ll * a * b / P * P;}
int a[N], b[N], c[N];
inline int read()
{
int x = 0, f = 1; char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}
inline void FWTor(int *a, int n, int t)
{
for (register int i = 1; i < n; i <<= 1)
for (register int j = 0; j < n; j += (i << 1))
for (register int k = 0; k < i; ++k)
if (~t) a[i + j + k] = add(a[i + j + k], a[j + k]);
else a[i + j + k] = sub(a[i + j + k], a[j + k]);
}
inline void FWTand(int *a, int n, int t)
{
for (register int i = 1; i < n; i <<= 1)
for (register int j = 0; j < n; j += (i << 1))
for (register int k = 0; k < i; ++k)
if (~t) a[j + k] = add(a[j + k], a[i + j + k]);
else a[j + k] = sub(a[j + k], a[i + j + k]);
}
inline void FWTxor(int *a, int n, int t)
{
for (register int i = 1; i < n; i <<= 1)
for (register int j = 0; j < n; j += (i << 1))
for (register int k = 0; k < i; ++k)
{
int x = a[j + k], y = a[i + j + k];
a[j + k] = add(x, y);
a[i + j + k] = sub(x, y);
if (!~t)
a[j + k] = mul(a[j + k], inv_2),
a[i + j + k] = mul(a[i + j + k], inv_2);
}
}
int main()
{
int n = read(), m = 1 << n;
for (register int i = 0; i < m; ++i) a[i] = read();
for (register int i = 0; i < m; ++i) b[i] = read();
FWTor(a, m, 1); FWTor(b, m, 1);
for (register int i = 0; i < m; ++i) c[i] = mul(a[i], b[i]);
FWTor(a, m, -1); FWTor(b, m, -1); FWTor(c, m, -1);
for (register int i = 0; i < m; ++i) printf("%d ", c[i]); puts("");
FWTand(a, m, 1); FWTand(b, m, 1);
for (register int i = 0; i < m; ++i) c[i] = mul(a[i], b[i]);
FWTand(a, m, -1); FWTand(b, m, -1); FWTand(c, m, -1);
for (register int i = 0; i < m; ++i) printf("%d ", c[i]); puts("");
FWTxor(a, m, 1); FWTxor(b, m, 1);
for (register int i = 0; i < m; ++i) c[i] = mul(a[i], b[i]);
FWTxor(a, m, -1); FWTxor(b, m, -1); FWTxor(c, m, -1);
for (register int i = 0; i < m; ++i) printf("%d ", c[i]); puts("");
return 0;
}
void FWT(LL *a,int n)
{
for (int i=1;i<n;i<<=1)
for (int p1=i<<1,j=0;j<n;j+=p1)
for (int k=0;k<i;k++) {
LL x=a[j+k]; LL y=a[j+k+i];
a[j+k]=(x+y)%p;
a[j+k+i]=(x-y+p)%p;
//xor: a[j+k]=x+y,a[j+k+i]=x-y
//and: a[j+k]=x+y
//or : a[j+k+i]=x+y
}
}
void UFWT(LL *a,int n)
{
for (int i=1;i<n;i<<=1)
for (int p1=i<<1,j=0;j<n;j+=p1)
for (int k=0;k<i;k++){
LL x=a[j+k]; LL y=a[j+k+i];
a[j+k]=(x+y)%p*ret%p;
a[j+k+i]=((x-y)*ret%p+p)%p;
//xor: a[j+k]=(x+y)/2,a[j+k+i]=(x-y)/2
//and: a[j+k]=x-y
//or : a[j+k+i]=y-x
}
}
void solve(LL *a,LL *b,int n)
{
FWT(a,n); FWT(b,n);
for (int i=0;i<n;i++) a[i]=a[i]*b[i]%p;
UFWT(a,n);
}