Reference http://www.cnblogs.com/v-July-v/archive/2011/08/13/2214132.html
"Calculation Guide"
The faster polynomial multiplication, then, relies on the ability to quickly convert a polynomial of a coefficient form into a point-value pair, and the form of a point-value pair is quickly transformed into a coefficient form. such as the following form:
The evaluation + pointwise multiplication + interpolation three-hop process.
#include <iostream> #include <string.h> #include <stdio.h> #include <math.h>using namespace std ; const int N = 500005;const Double PI = ACOs ( -1.0); struct virt{double R, I; Virt (Double r = 0.0,double i = 0.0) {this->r = R; This->i = i; } Virt operator + (const Virt &x) {return Virt (R + X.R, i + x.i); } Virt Operator-(const Virt &x) {return Virt (R-X.R, i-x.i); } Virt operator * (const Virt &x) {return Virt (R * x.r-i * x.i, I * X.R + R * x.i); }};//Reid algorithm--inverted-order void Rader (Virt f[], int len) {int j = Len >> 1; for (int i=1; i<len-1; i++) {if (I < j) Swap (F[i], f[j]); int k = Len >> 1; while (J >= K) {J-= k; K >>= 1; } if (J < k) J + = k; }}//fft implements a void FFT (Virt f[], int len, int on) {rader (F, Len); for (int h=2; h<=len; h<<=1)//divided by the calculation of the length of H DFT {VIRT wn (cos (-on*2*pi/h), sin (-on*2*pi/h)); Unit complex root e^ (2*pi/m) uses Euler formula to expand for (int j=0; j<len; j+=h) {Virt W (1,0); Rotation factor for (int k=j; k<j+h/2; k++) {Virt u = f[k]; Virt t = w * f[k + H/2]; F[k] = U + t; Butterfly merge operation F[k + H/2] = u-t; w = w * WN; Update rotation Factor}} if (on = =-1) for (int i=0; i<len; i++) f[i].r/= Len;} convolution void Conv (Virt a[],virt b[],int len) {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);} Char Str1[n],str2[n]; Virt va[n],vb[n];int result[n];int len;void Init (char Str1[],char str2[]) {int len1 = strlen (STR1); int len2 = strlen (STR2); len = 1; while (Len < 2*len1 | | Len < 2*LEN2) Len <<= 1; int i; for (i=0; i<len1; i++) {VA[I].R = str1[len1-i-1]-' 0 '; va[i].i = 0.0; } while (I < len) {VA[I].R = va[i].i = 0.0; i++; } for (i=0; i<len2; i++) {VB[I].R = str2[len2-i-1]-' 0 '; vb[i].i = 0.0; } while (I < len) {VB[I].R = vb[i].i = 0.0; i++; }}void work () {Conv (Va,vb,len); for (int i=0; i<len; i++) result[i] = va[i].r+0.5;} void Export () {for (int i=0; i<len; i++) {result[i+1] + = RESULT[I]/10; Result[i]%= 10; } int high = 0; for (int i=len-1, i>=0; i--) {if (Result[i]) {high = i; Break }} for (int i=high; i>=0; i--) printf ("%d", result[i]); Puts ("");} int main () {while (~scanf ("%s%s", Str1,str2)) {Init (STR1,STR2); Work (); Export (); } return 0;}
Fast Fourier transform &hdu 1402