Mathematics (iii) |
Difficulty level: D; operating time limit: 1800ms; operating space limit: 262144KB; code length limit: 2000000B |
Question Description |
Give two positive integers, a, B, and ask for a*b. |
Input |
Enter a total of two lines, each of which is a positive integer. |
Output |
Outputs a total line, which is their product. |
Input example |
2 2 |
Output example |
4 |
Other Notes |
0<a,b<=10^200000 |
Classical problem, accelerating large integer multiplication with FFT.
#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i!=-1;i=next[i])using namespaceStd;inlineintRead () {intx=0, f=1;CharC=GetChar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=800010;Const DoublePi=acos (-1.0);structFFT {structCox {DoubleR,i; Cox (Double_r=0.0,Double_i=0.0) {r=_r;i=_i;} Coxoperator+ (Constcox& b) {returnCox (r+b.r,i+B.I);} Coxoperator- (Constcox& b) {returnCox (r-b.r,i-B.I);} Coxoperator* (Constcox& b) {returnCox (r*b.r-i*b.i,r*b.i+i*B.R);} }F[MAXN]; intLen; voidInitCharAintLintLen) {Len=l;rep (I,0, len-1) F[i]=cox (a[len-i-1]-'0',0); } voidFftintTP) { intJ=len>>1; Rep (I,1, len-2) { if(i<j) Swap (f[i],f[j]);intK=len>>1; while(j>=k) j-=k,k>>=1; j+=K; } Doublelm=-2*tp*PI; for(intI=2; i<=len;i<<=1) {Cox wn (cos (LM/i), sin (lm/i)); for(intj=0; j<len;j+=i) {Cox W (1,0); for(intk=j;k<j+ (i>>1); k++) {Cox u=f[k],v=w*f[k+ (i>>1)]; F[K]=u+v;f[k+ (i>>1)]=u-v;w=w*WN; } } } if(tp<0) Rep (I,0, len-1) f[i].r/=Len; }}a,b;voidMulCharACharBintL1,intL2,int& L,int*ans) {L=1; while(l<l1<<1|| l<l2<<1) l<<=1; A.init (A,L,L1); B.init (B,L,L2); A.fft (1); B.fft (1); Rep (I,0, L-1) a.f[i]=a.f[i]*B.f[i]; A.fft (-1); Rep (I,0, L-1) ans[i]=int(a.f[i].r+0.5);}CharA[MAXN],B[MAXN];intANS[MAXN];intMain () {scanf ("%s%s", A, b); intL1=strlen (A), l2=strlen (B), L; Mul (A,b,l1,l2,l,ans); Rep (I,0, L-2) {ans[i+1]+=ans[i]/Ten; Ans[i]%=Ten; } while(l>1&&!ans[l-1]) l--; Dwn (i,l-1,0) Putchar (ans[i]+'0'); return 0;}
View Code
COJ0702 Math (c)