1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include<string> #include<cstdio> #include<math.h> #include<iostream> #include<string.h> #include <algorithm> using namespace std; #define ll long long const double pi = acos(-1); const int N = 3e5+10; char s1[50010],s2[50010]; int a[N],b[N],r[N],res[N]; struct cp { double r,i; cp(double _r = 0,double _i = 0) { r = _r; i = _i; } cp operator +(const cp &b) { return cp(r+b.r,i+b.i); } cp operator -(const cp &b) { return cp(r-b.r,i-b.i); } cp operator *(const cp &b) { return cp(r*b.r-i*b.i,r*b.i+i*b.r); } }A[N],B[N]; void FFT(cp *s,int n,int inv){ for(int i=0;i<n;i++){ if(i<r[i])swap(s[i],s[r[i]]); } for(int len=2;len<=n;len<<=1){ cp wn = cp(cos(2*pi/len),inv*sin(2*pi/len)); for(int st=0;st<n;st+=len){ cp w = cp(1.0,0.0); for(int i=st;i<st+len/2;i++,w=w*wn){ cp x=s[i]; cp y=w*s[i+len/2]; s[i]=x+y; s[i+len/2]=x-y; } } } if(inv==-1){ for(int i=0;i<n;i++) s[i].r/=n; } } void solve(){ int len =1,bit=0; int s1len=strlen(s1); int s2len=strlen(s2); int slen = max(s1len,s2len); while(len<slen*2)len<<=1,bit++; for(int i=0;i<len;i++){ a[i]=b[i]=r[i]=res[i]=0; A[i]=B[i]=cp(0.0,0.0); } for(int i=0;i<s1len;i++)a[i]=s1[i]-'0',A[i].r=a[i]; for(int i=0;i<s2len;i++)b[i]=s2[i]-'0',B[i].r=b[i]; for(int i=0;i<len;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(bit-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++)res[i]=(int)(A[i].r+0.5); for(int i=len-1;i>=1;i--){ if(res[i]>=10)res[i-1]+=res[i]/10,res[i]%=10; } if(res[0]==0)puts("0"); else { for(int i=0;i<s1len+s2len-1;i++) { printf("%d",res[i]); } printf("\n"); }
} int main(){ while(scanf("%s %s",s1,s2)!=EOF){ solve(); } return 0; }
|