## 模板

### $FFT$模板

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}

const int MAXN=1000005;
const int MAXLEN=2100005;
const double pi=std::acos(-1);
int n,m;
int len,rev[MAXLEN];
struct Complex{
double real,imag;
inline friend Complex operator + (Complex x,Complex y){
return (Complex){x.real+y.real,x.imag+y.imag};
}
inline friend Complex operator - (Complex x,Complex y){
return (Complex){x.real-y.real,x.imag-y.imag};
}
inline friend Complex operator * (Complex x,Complex y){
return (Complex){x.real*y.real-x.imag*y.imag,x.real*y.imag+x.imag*y.real};
}
};
Complex A[MAXLEN],B[MAXLEN];

inline void fft(Complex *c,int dft){
rin(i,0,n-1) if(i<rev[i])
std::swap(c[i],c[rev[i]]);
for(int mid=1;mid<n;mid<<=1){
int r=(mid<<1);
Complex u=(Complex){std::cos(pi/mid),dft*std::sin(pi/mid)};
for(int l=0;l<n;l+=r){
Complex v=(Complex){1,0};
for(int i=0;i<mid;i++,v=v*u){
Complex x=c[l+i],y=c[l+mid+i]*v;
c[l+i]=x+y;
c[l+mid+i]=x-y;
}
}
}
if(dft<0) rin(i,0,n-1)
c[i].real/=n;
}

int main(){
m+=n;
for(n=1;n<=m;n<<=1) len++;
rin(i,1,n-1) rev[i]=((rev[i>>1]>>1)|((i&1)<<(len-1)));
fft(A,1);
fft(B,1);
rin(i,0,n-1) A[i]=A[i]*B[i];
fft(A,-1);
rin(i,0,m) printf("%d ",(int)(A[i].real+0.5));
printf("\n");
return 0;
}


### $NTT$模板

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}

const int MAXN=1000005;
const int MAXLEN=2100005;
const LL MOD=998244353,G=3,INVG=332748118;
int n,m,invn;
int len,rev[MAXLEN];
LL A[MAXLEN],B[MAXLEN];

inline LL qpow(LL x,LL y){
LL ret=1,tt=x%MOD;
while(y){
if(y&1) ret=ret*tt%MOD;
tt=tt*tt%MOD;
y>>=1;
}
return ret;
}

inline void ntt(LL *c,int dft){
rin(i,0,n-1) if(i<rev[i])
std::swap(c[i],c[rev[i]]);
for(int mid=1;mid<n;mid<<=1){
int r=(mid<<1);
LL u=qpow(dft>0?G:INVG,(MOD-1)/r);
for(int l=0;l<n;l+=r){
LL v=1;
for(int i=0;i<mid;i++,v=v*u%MOD){
LL x=c[l+i],y=c[l+mid+i]*v%MOD;
c[l+i]=x+y;
if(c[l+i]>=MOD) c[l+i]-=MOD;
c[l+mid+i]=x-y;
if(c[l+mid+i]<0) c[l+mid+i]+=MOD;
}
}
}
if(dft<0) rin(i,0,n-1)
c[i]=c[i]*invn%MOD;
}

int main(){
m+=n;
for(n=1;n<=m;n<<=1) len++;
rin(i,1,n-1) rev[i]=((rev[i>>1]>>1)|((i&1)<<(len-1)));
invn=qpow(n,MOD-2);
ntt(A,1);
ntt(B,1);
rin(i,0,n-1) A[i]=A[i]*B[i]%MOD;
ntt(A,-1);
rin(i,0,m) printf("%lld ",A[i]);
printf("\n");
return 0;
}


### $FWT$模板

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;

int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}

const int MAXN=135005;
const int MOD=998244353;
const int INV2=499122177;
int n;
int len;
int a[MAXN],b[MAXN];
int A[MAXN],B[MAXN],C[MAXN],D[MAXN];

inline void fwt(int *c,const char *s,int opt){
for(int mid=1;mid<n;mid<<=1){
int r=(mid<<1);
for(int l=0;l<n;l+=r){
for(int i=0;i<mid;i++){
if(s=="or"){
if(opt>0){
c[l+mid+i]+=c[l+i];
if(c[l+mid+i]>=MOD) c[l+mid+i]-=MOD;
}
else{
c[l+mid+i]-=c[l+i];
if(c[l+mid+i]<0) c[l+mid+i]+=MOD;
}
}
else if(s=="and"){
if(opt>0){
c[l+i]+=c[l+mid+i];
if(c[l+i]>=MOD) c[l+i]-=MOD;
}
else{
c[l+i]-=c[l+mid+i];
if(c[l+i]<0) c[l+i]+=MOD;
}
}
else{
LL x=c[l+i],y=c[l+mid+i];
c[l+i]=x+y;
c[l+mid+i]=x-y;
if(opt<0){
c[l+i]=1ll*c[l+i]*INV2%MOD;
c[l+mid+i]=1ll*c[l+mid+i]*INV2%MOD;
}
if(c[l+i]>=MOD) c[l+i]-=MOD;
if(c[l+mid+i]<0) c[l+mid+i]+=MOD;
}
}
}
}
}

int main(){
n=(1<<len);
fwt(A,"or",1);
fwt(D,"or",1);
rin(i,0,n-1) A[i]=1ll*A[i]*D[i]%MOD;
fwt(A,"or",-1);
rin(i,0,n-1) D[i]=b[i];
fwt(B,"and",1);
fwt(D,"and",1);
rin(i,0,n-1) B[i]=1ll*B[i]*D[i]%MOD;
fwt(B,"and",-1);
rin(i,0,n-1) D[i]=b[i];
fwt(C,"xor",1);
fwt(D,"xor",1);
rin(i,0,n-1) C[i]=1ll*C[i]*D[i]%MOD;
fwt(C,"xor",-1);
rin(i,0,n-1) printf("%d ",A[i]);
printf("\n");
rin(i,0,n-1) printf("%d ",B[i]);
printf("\n");
rin(i,0,n-1) printf("%d ",C[i]);
printf("\n");
return 0;
}


## $FFT/NTT$习题

[BZOJ2179]FFT快速傅立叶

$FFT$优化高精乘，因为没有取模的问题所以可能在这里$NTT$的常数要优于$FFT$

[BZOJ2194]快速傅立叶之二

$b$数组$reverse()$，发现原来的式子变成了：

$C[k]=\sum(a[i] \times b[n+k-1-i])$

[BZOJ3527]力

[BZOJ3160]万径人踪灭

[BZOJ4503]两个串

[BZOJ2194]快速傅立叶之二出发，判断两个字符串是否匹配可以通过作差后平方转化为卷积的形式。由于通配符的存在外面还需要再乘一个$T[i]$

[BZOJ4827][Hnoi2017]礼物

$c$的最优值一定为二次函数顶点，剩下的就是一个卷积了。

[HDU4609]3-idiots

[BZOJ3625][Codeforces Round #250]小朋友和二叉树

$F(x) \equiv F(x)^2 \times G(x)+1\ (mod\ x^{m+1})$

$G(x)$$c$的生成函数。

[BZOJ3509][CodeChef]COUNTARI

$2 \times A[j]=A[i]+A[k]$

[BZOJ3771]Triple

[UVA12633]Super Rooks on Chessboard

[HDU5307]He is Flying

$F(x)=(\sum_{i=1}^nix^{sum_i}) \times (\sum_{i=1}^nx^{-sum_{i-1}})-(\sum_{i=1}^nx^{sum_i}) \times (\sum_{i=1}^n(i-1)x^{sum_{i-1}})$

## $FWT$习题

[HDU5909]Tree Cutting

$F[i](x)$表示以$i$为根的子树的生成函数，树形$DP$，合并时使用$FWT$即可。

[BZOJ4589]Hard Nim

[CF662C]Binary Table

$F(x)=G(x) \oplus H(x)$$F(x)$最小的系数即为答案。

[BZOJ4036][HAOI2015]按位或

$E(min\{T\})$可以通过补集求，需要用到$FWT$

posted on 2018-11-26 21:47  ErkkiErkko  阅读(246)  评论(0编辑  收藏

• 随笔 - 91
• 文章 - 0
• 评论 - 20