FFT makes high-precision multiplication
1#include <bits/stdc++.h>2 3 Const DoublePI = ACOs (-1);4 5 structComplex6 {7 DoubleA, B;8 9 Inline Complex (Ten Double_a =0, One Double_b =0) A { -A =_a; -b =_b; the } - -Inline friend complexoperator+ -(ConstComplex &a,ConstComplex &b) + { - returnComplex (A.A + B.A, a.b +b.b); + } A atInline friend complexoperator- -(ConstComplex &a,ConstComplex &b) - { - returnComplex (A.A-B.A, A.B-b.b); - } - inInline friend complexoperator* -(ConstComplex &a,ConstComplex &b) to { + returnComplex (a.a*b.a-a.b*b.b, a.a*b.b + a.b*B.A); - } the *Inline Friend Complex &operator+= $(Complex &a,ConstComplex &b)Panax Notoginseng { - returnA = a +b; the } + AInline Friend Complex &operator-= the(Complex &a,ConstComplex &b) + { - returnA = A-b; $ } $ -Inline Friend Complex &operator*= -(Complex &a,ConstComplex &b) the { - returnA = A *b;Wuyi } the }; - WuInline Complex Alpha (Doublea) - { About returnComplex (cos (a), sin (a)); $ } - -typedef std::vector<complex>Vec; - AVEC FFT (ConstVEC &a) + { the intn =a.size (); - $ if(n = =1)returnA; the theComplex W_k (1,0); theComplex w_n = Alpha (pi*2/n); the -VEC ar[2], yr[2], y (n); in the for(inti =0; I < n; ++i) theAr[i &1].push_back (A[i]); About the for(inti =0; I <2; ++i) theYr[i] =FFT (Ar[i]); the + for(inti =0; I < n/2; ++i, W_k *=w_n) - { theY[i] = yr[0][i] + w_k * yr[1][i];BayiY[i + n/2] = yr[0][i]-W_k * yr[1][i]; the } the - returny; - } the theVEC IFFT (ConstVEC &a) the { the intn =a.size (); - the if(n = =1)returnA; the theComplex W_k (1,0);94Complex w_n = Alpha (-pi*2/n); the theVEC ar[2], yr[2], y (n); the 98 for(inti =0; I < n; ++i) AboutAr[i &1].push_back (A[i]); - 101 for(inti =0; I <2; ++i)102Yr[i] =IFFT (Ar[i]);103 104 for(inti =0; I < n/2; ++i, W_k *=w_n) the {106Y[i] = yr[0][i] + w_k * yr[1][i];107Y[i + n/2] = yr[0][i]-W_k * yr[1][i];108 }109 the returny;111 } the 113 Chars1[100005];intlen1; the Chars2[100005];intLen2; the the VEC v1, v2, p1, p2, mul, ans;117 118Signed Main (void)119 { -scanf"%s", S1); Len1 =strlen (S1);121scanf"%s", S2); Len2 =strlen (S2);122 123 intLen = len1 +Len2;124 the while(Len! = (Len&-len)) + +Len;126 127 for(inti = len1-1; ~i; I.) V1.push_back (Complex (S1[i)-'0',0)); - for(inti = len2-1; ~i; I.) V2.push_back (Complex (S2[i)-'0',0));129 the while((int) V1.size () <len) V1.push_back (complex ());131 while((int) V2.size () <len) V2.push_back (complex ()); the 133P1 =FFT (v1);134P2 =FFT (v2);135 136 for(inti =0; i < Len; ++i)137Mul.push_back (P1[i] *p2[i]);138 139Ans =IFFT (mul); $ 141std::vector<int>ret;142 143 for(inti =0; i < Len; ++i)144Ret.push_back ((int) Round (ANS[I].A/len));145 146 for(inti =0; i < Len; ++i)147 if(Ret[i] >=Ten)148 {149Ret[i +1] + = Ret[i]/Ten; MaxRet[i]%=Ten;151 } the 153 while(Ret.size ()! =1&&!ret[ret.size ()-1])154 Ret.pop_back ();155 156 for(inti = ret.size ()-1; I >=0; --i)157Putchar ('0'+ret[i]);158Putchar ('\ n');159}
@Author: Yousiki
CODE[VS] 3123 super-large integer multiplication of high-precision exercises