Spoj-vfmul:https://vjudge.net/problem/spoj-vfmul
This is an FFT for high-precision template problem.
Reference: https://www.cnblogs.com/RabbitHu/p/FFT.html
#include <algorithm>#include<iterator>#include<iostream>#include<cstring>#include<cstdlib>#include<iomanip>#include<complex>#include<bitset>#include<cctype>#include<cstdio>#include<string>#include<vector>#include<stack>#include<cmath>#include<queue>#include<list>#include<map>#include<Set>#include<cassert>using namespacestd;//#pragma GCC optimize (3)//#pragma COMMENT (linker, "/stack:102400000,102400000")//C + +//#pragma GCC diagnostic error "-std=c++11"//#pragma COMMENT (linker, "/stack:200000000")//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")//#pragma GCC optimize ("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations, -fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions, Inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions, no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,- falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,- fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,- fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,ofast,inline,- Fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2 ", 3) #defineLson (L, Mid, RT << 1)#defineRson (mid + 1, R, RT << 1 | 1)#defineDebug (x) cerr << #x << "=" << x << "\ n";#definePB Push_back#definePQ Priority_queuetypedefLong Longll;typedef unsignedLong Longull; typedef pair<ll, LL >Pll;typedef pair<int,int>Pii;typedef pair<int,pii>P3;typedef Complex<Double>CP;//priority_queue<int> Q;//This is a big root heap Q//priority_queue<int,vector<int>,greater<int> >q;//This is a little Gan q#defineFi first#defineSe Second//#define Endl ' \ n ' #defineOKC Ios::sync_with_stdio (false); Cin.tie (0)#defineFT (A,B,C) for (int a=b; A <= C;++a)//used to press the line#defineREP (I, J, K) for (int i = j; i < K; ++i)#defineMax3 (A,B,C) max (max (b), c);//Priority_queue<int, Vector<int>, greater<int> >que; Constll mos =0x7FFFFFFF;//2147483647Constll nMOS =0x80000000;//-2147483648Const intINF =0x3f3f3f3f;Constll INFF =0x3f3f3f3f3f3f3f3f;// -//const INT mod = 10007;Const DoubleESP = 1e-8;Const DoublePi=acos (-1.0);Const DoublePhi=0.61803399;//Golden Split PointConst DoubleTphi=0.38196601; Template<typename t>inline T read (t&x) {x=0;intf=0;CharCh=GetChar (); while(ch<'0'|| Ch>'9') f|= (ch=='-'), ch=GetChar (); while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); returnx=f?-x:x;} /*-----------------------Showtime----------------------*/ Const intMAXN =2*600009;//note cannot and len1+len2 and just good, because N is the factorial of the nearest 2CP A[MAXN], B[MAXN], OMG[MAXN], INV[MAXN]; intn=1; voidinit () {memset (OMG,0,sizeof(OMG)); memset (INV,0,sizeof(INV)); for(inti =0; I < n; i++) {Omg[i]= CP (COS (2* PI * i/n), sin (2* PI * I/N)); Inv[i]=Conj (Omg[i]); } } voidFFT (CP *a, CP *OMG) { intLim =0; while((1<< Lim) < n) lim++; for(inti =0; I < n; i++){ intt =0; for(intj =0; J < Lim; J + +) if((i >> J) &1) T |= (1<< (Lim-j-1)); if(I <t) swap (A[i], a[t]); } for(intL =2; L <= N; L *=2){ intm = L/2; for(cp *p = A; P! = a + N; p + =l) for(inti =0; I < m; i++) {CP T= omg[n/l * I] * p[i +m]; P[i+ M] = P[i]-T; P[i]+=T; } } } CharS1[MAXN],S2[MAXN]; intRES[MAXN];intMain () {intT;SCANF ("%d", &T); while(t--) {n=1; scanf ("%s%s", S1, S2); intLen1 = strlen (S1), len2 =strlen (S2); while(N < len1 + len2) n <<=1; Memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); for(intI=0; i<len1; i++) {A[len1I1].real (S1[i]-'0'); } for(intI=0; i<len2; i++) {B[len2I1].real (S2[i]-'0'); } init (); FFT (A, OMG); FFT (b, OMG); for(intI=0; i<n; i++) {A[i]*=B[i]; } FFT (A,INV); memset (Res,0,sizeof(res)); for(intI=0; i<n; i++) {Res[i]+=Double(A[i].real ()/n +0.5); Res[i+1] + = Res[i]/Ten; Res[i]= Res[i]%Ten; } ints = len1 + Len2-1; //debug (s); while(Res[s] = =0&& s >0) s--; for(inti = s; i>=0; i--) {Putchar ('0'+Res[i]); } printf ("\ n"); } return 0;}
FFT
Spoj-vfmul-very fast multiplication FFT accelerates high-precision multiplication