菜鸡求助!! 第一个点re

P1303 A*B Problem

111l @ 2019-11-09 15:14:12

rt,用FFT做的,本地测试能过,感激不尽QAQ。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const double pi=acos(-1);
int n,m,r[1001],h[1001];
char x[1001],y[1001];
struct cp{
    double x,y;
    cp(double a=0,double b=0){x=a,y=b;}
    cp operator +(cp &a){return cp(x+a.x,y+a.y);}
    cp operator -(cp &a){return cp(x-a.x,y-a.y);}
    cp operator *(cp &a){return cp(x*a.x-y*a.y,x*a.y+y*a.x);}
}f[1001],g[1001];
void fft(cp *f,int sz,bool fg){
    if(sz==1) return;
    fft(f,sz>>1,fg),fft(f+(sz>>1),sz>>1,fg);
    cp a(cos(2*pi/sz),sin(2*pi/sz)),tmp(1,0);
    if(!fg) a.y*=-1;
    for(int i=0;i<(sz>>1);i++){
        cp t=tmp*f[i+(sz>>1)];
        f[i+(sz>>1)]=f[i]-t;
        f[i]=f[i]+t,tmp=tmp*a;
    }
}
int main(){
    scanf("%s%s",x,y);
    if(y[0]=='0'||x[0]=='0') return printf("0");
    n=strlen(x);
    m=strlen(y);
    --n,--m;
    for(int i=0;i<=n;f[i].x=x[i]-48,i++);
    for(int i=0;i<=m;g[i].x=y[i]-48,i++);
    for(m+=n,n=1;n<=m;n<<=1);
    for(int i=0;i<n;r[i]=(r[i>>1]>>1)|((i&1)?n>>1:0),i++);
    for(int i=0;i<n;i++) if(i<r[i]) swap(f[i],f[r[i]]),swap(g[i],g[r[i]]);
    fft(f,n,1),fft(g,n,1);
    for(int i=0;i<n;i++) f[i]=f[i]*g[i];
    for(int i=0;i<n;i++) if(i<r[i]) swap(f[i],f[r[i]]);
    fft(f,n,0);
    for(int i=0;i<=m;h[i+1]=(int)(f[i].x/n+0.49),i++);
    for(int i=m+1;i;i--) if(h[i]>=10) h[i-1]+=h[i]/10,h[i]%=10;
    if(h[0]) for(int i=0;i<=m+1;i++) printf("%d",h[i]);
    else for(int i=1;i<=m+1;i++) printf("%d",h[i]);
}

by 超级小周 @ 2019-11-09 15:17:19

我的思路:嗯,我想想—FFT?—A*B?(


by NaCly_Fish @ 2019-11-09 15:18:37

@陈沫のkiss FFT 就是高精度乘法的优化啊,,


by Callous_Murder @ 2019-11-09 15:20:06

@NaCly_Fish %%% 好吧,我弱


by 111l @ 2019-11-09 15:21:22

所以到底是什么问题??


by 111l @ 2019-11-09 15:21:45

本地测试是能过的


|