【BZOJ4836】[Lydsy1704月赛]二元运算(分治FFT)

传送门

显然权值转下标后分治FFTFFTFFT即可

利用MTTMTTMTT的技巧可以做到DFTDFTDFT次数减半

卡到bzoj rk2bzoj\ rk2bzoj rk2
不知到第一是怎么做到2k代码跑那么快的

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=200005;
ll ans[N];
int A[N],B[N];
namespace Poly{
cs int C=18,L=(1<<C)+1;
cs double pi=acos(-1);
struct plx{
    double x,y;
    plx(double _x=0,double _y=0):x(_x),y(_y){}
    friend inline plx operator +(cs plx &a,cs plx &b){
        return plx(a.x+b.x,a.y+b.y);
    }
    friend inline plx operator -(cs plx &a,cs plx &b){
        return plx(a.x-b.x,a.y-b.y);
    }
    friend inline plx operator *(cs plx &a,cs plx &b){
        return plx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
    }
    friend inline plx operator /(cs plx &a,cs int &b){
        return plx(a.x/b,a.y/b);
    }
    inline plx conj(){return plx(x,-y);}
};
plx *w[C+1];
int rev[L];
inline void init_w(){
    for(int i=1;i<=C;i++)w[i]=new plx[(1<<(i-1))+1];
    plx wn=plx(cos(pi/(1<<(C-1))),sin(pi/(1<<(C-1))));
    w[C][0]=plx(1,0);
    for(int i=1;i<(1<<(C-1));i++){
        if(i&31)w[C][i]=w[C][i-1]*wn;
        else w[C][i]=plx(cos(i*pi/(1<<(C-1))),sin(i*pi/(1<<(C-1))));
    }
    for(int i=C-1;i;i--)
    for(int j=0;j<(1<<(i-1));j++)w[i][j]=w[i+1][j<<1];
}
inline void init_rev(int lim){
    for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline void fft(plx *f,int lim,int kd){
    for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
    plx a0,a1;
    for(int mid=1,l=1;mid<lim;mid<<=1,l++)
    for(int i=0;i<lim;i+=mid<<1)
    for(int j=0;j<mid;j++)
    a0=f[i+j],a1=f[i+j+mid]*w[l][j],f[i+j]=a0+a1,f[i+j+mid]=a0-a1;
    if(kd==-1){
        reverse(f+1,f+lim);
        for(int i=0;i<lim;i++)f[i]=f[i]/lim;
    }
}
#define poly vector<plx>
inline poly operator *(poly a,poly b){
    int deg=a.size()+b.size()-1,lim=1;
    while(lim<deg)lim<<=1;
    init_rev(lim);
    a.resize(lim),b.resize(lim);
    fft(&a[0],lim,1),fft(&b[0],lim,1);
    for(int i=0;i<lim;i++)a[i]=a[i]*b[i];
    fft(&a[0],lim,-1),a.resize(deg);
    return a;
}
inline void mul(int *A,int *B,int l,int r){
    static plx a[L],b[L],c[L],da,db,dc,dd;
    int mid=(l+r)>>1,deg=r-l+1,lim=1;
    while(lim<deg)lim<<=1;
    init_rev(lim);
    for(int i=0;i<lim;i++)a[i]=plx(0,0),b[i]=plx(0,0),c[i]=plx(0,0);
    for(int i=l;i<=mid;i++)a[i-l].x=A[i],b[mid-i].y=B[i];
    for(int i=mid+1;i<=r;i++)b[i-mid-1].x=A[i],a[i-mid-1].y=B[i];
    fft(a,lim,1),fft(b,lim,1);
    for(int i=0;i<lim;i++){
        int j=(lim-i)&(lim-1);
        da=(a[i]+a[j].conj())*plx(0.5,0);
        dd=(a[j].conj()-a[i])*plx(0,0.5);
        db=(b[i]+b[j].conj())*plx(0.5,0);
        dc=(b[j].conj()-b[i])*plx(0,0.5);
        c[i]=(da*dd)+((db*dc)*plx(0,1));
    }
    fft(c,lim,-1);
    for(int i=0;i<deg;i++)ans[l+mid+1+i]+=(ll)(c[i].x+0.5),ans[i+1]+=(ll)(c[i].y+0.5);
}
}
using Poly::init_w;
using Poly::mul;
inline void calc(int l,int r){
    if(r-l+1<=32){
        for(int i=l;i<=r;i++)
        for(int j=l;j<=r;j++){
            if(i<j)ans[i+j]+=1ll*A[i]*B[j];
            else ans[i-j]+=1ll*A[i]*B[j];
        }return;
    }
    int mid=(l+r)>>1;     
    mul(A,B,l,r);
    calc(l,mid),calc(mid+1,r);
}
int n,m,q;
inline void solve(){
    memset(ans,0,sizeof(ans));
    memset(A,0,sizeof(A));
    memset(B,0,sizeof(B));
    int mx=0;
    n=read(),m=read(),q=read();
    for(int i=1,x;i<=n;i++)x=read(),chemx(mx,x),A[x]++;
    for(int i=1,x;i<=m;i++)x=read(),chemx(mx,x),B[x]++;
    calc(0,mx);
    while(q--)cout<<ans[read()]<<'\n';
}
int main(){
    #ifdef Stargazer
    freopen("lx.in","r",stdin);
    #endif
    init_w();
    int T=read();
    while(T--)solve();
}
【BZOJ4836】[Lydsy1704月赛]二元运算(分治FFT)【BZOJ4836】[Lydsy1704月赛]二元运算(分治FFT) Backseat-stargazer 发布了545 篇原创文章 · 获赞 191 · 访问量 4万+ 他的留言板 关注
上一篇:迷宫问题 Maze 4X4 (sloved by backtrack)


下一篇:[高等数学]解析一道关于函数极限的概念考察题(001)