P1919 【模板】A*B Problem升级版(FFT快速傅里叶)

传送门

注意读入是从高位到低位的...

输出记得判前导零...

然后就是模板了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=240007;
const double pi=acos(-1.0);
struct CP{
    double x,y;
    CP (double xx=0,double yy=0) { x=xx,y=yy; }
    inline CP operator + (const CP &tmp) const {
        return CP(x+tmp.x,y+tmp.y);
    }
    inline CP operator - (const CP &tmp) const {
        return CP(x-tmp.x,y-tmp.y);
    }
    inline CP operator * (const CP &tmp) const {
        return CP(x*tmp.x-y*tmp.y,x*tmp.y+y*tmp.x);
    }
}A[N],B[N];
int n,p[N],ans[N];
void FFT(CP *A,int len,int type)
{
    for(int i=0;i<len;i++) if(i<p[i]) swap(A[i],A[p[i]]);
    for(int mid=1;mid<len;mid<<=1)
    {
        CP wn(cos(pi/mid),type*sin(pi/mid));
        for(int R=mid<<1,j=0;j<len;j+=R)
        {
            CP w(1,0);
            for(int k=0;k<mid;k++,w=w*wn)
            {
                CP x=A[j+k],y=w*A[j+mid+k];
                A[j+k]=x+y;
                A[j+mid+k]=x-y;
            }
        }
    }
}
char s[N];
int main()
{
    n=read()-1; int len=1,tot=0;
    scanf("%s",s);
    for(int i=0;i<=n;i++) A[i].x=s[n-i]-'0';
    scanf("%s",s);
    for(int i=0;i<=n;i++) B[i].x=s[n-i]-'0';
    while(len<=n+n) len<<=1,tot++;
    for(int i=0;i<len;i++) p[i]=(p[i>>1]>>1) | ((i&1)<<(tot-1));
    FFT(A,len,1); FFT(B,len,1);
    for(int i=0;i<=len;i++) A[i]=A[i]*B[i];
    FFT(A,len,-1);
    for(int i=0;i<=len;i++)
    {
        ans[i]+=(int)(A[i].x/len+0.5);
        if(ans[i]>9) ans[i+1]+=ans[i]/10,ans[i]%=10,len+=(i==len);
    }
    while(!ans[len]) len--;
    for(int i=len;i>=0;i--) printf("%d",ans[i]);
    printf("\n");
    return 0;
}

 

posted @ 2019-07-16 14:43  LLTYYC  阅读(168)  评论(0编辑  收藏  举报