标签:mes alc 答案 targe 参考 1.5 strlen -- height
给出两个超级大的整数,求出a*b
Rose_max出的一道FFT例题,卡掉高精度 = =
只要把a和b的每一位当作是多项式的系数,然后做FFT就好了
然后将答案取下来,进行进位的操作,最后输出就好了
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; const double PI=acos(-1.0); struct Complex { double r,i; Complex(){} Complex(double _r,double _i){r=_r;i=_i;} friend Complex operator + (const Complex &x,const Complex &y){return Complex(x.r+y.r,x.i+y.i);} friend Complex operator - (const Complex &x,const Complex &y){return Complex(x.r-y.r,x.i-y.i);} friend Complex operator * (const Complex &x,const Complex &y){return Complex(x.r*y.r-x.i*y.i,x.i*y.r+x.r*y.i);} }a[410000],b[410000]; int n,m; int R[410000]; void fft(Complex *y,int len,int on) { for(int i=0;i<len;i++) if(i<R[i]) swap(y[i],y[R[i]]); for(int i=1;i<len;i<<=1) { Complex wn(cos(PI/i),sin(on*PI/i)); for(int j=0;j<len;j+=(i<<1)) { Complex w(1,0); for(int k=0;k<i;k++,w=w*wn) { Complex u=y[j+k]; Complex v=w*y[j+k+i]; y[j+k]=u+v; y[j+k+i]=u-v; } } } if(on==-1) for(int i=0;i<len;i++) y[i].r/=len; } void calc() { m+=n; int L=0; for(n=1;n<=m;n<<=1) L++; for(int i=0;i<n;i++) R[i]=(R[i>>1]>>1)|(i&1)<<(L-1); fft(a,n,1); fft(b,n,1); for(int i=0;i<=n;i++) a[i]=a[i]*b[i]; fft(a,n,-1); } char st[110000]; int d[410000]; int main() { scanf("%s",st+1); n=strlen(st+1);n--; for(int i=0;i<=n;i++) a[i].r=double(st[i+1]-‘0‘); scanf("%s",st+1); m=strlen(st+1);m--; for(int i=0;i<=m;i++) b[i].r=double(st[i+1]-‘0‘); calc(); for(int i=0;i<=m;i++) d[i]=int(a[m-i].r+0.5); for(int i=0;i<=m;i++) { d[i+1]+=d[i]/10; d[i]%=10; } int i=m; while(d[i+1]!=0) { i++; d[i+1]+=d[i]/10; d[i]%=10; } m=i; for(int i=m;i>=0;i--) printf("%d",d[i]); printf("\n"); return 0; }
标签:mes alc 答案 targe 参考 1.5 strlen -- height
原文地址:https://www.cnblogs.com/Never-mind/p/8979679.html