2179:fft Fast Fourier time limit:10 Sec Memory limit:259 MB
submit:2978 solved:1523
[Submit] [Status] [Discuss] Description gives two n-bit 10 binary integers x and y, you need to calculate x*y. Input first line a positive integer n. The second line describes a positive integer x with the number of bits N. The third line describes a positive integer y with the number of bits N. Output outputs a row, which is the result of x*y. Sample Input1
3
4
Sample Output12
Data range:
n<=60000
Hintsourcesolution
Rt.. Leave a board.
Code
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < Algorithm>using namespace Std;struct complex{double r,i; Complex (double r=0.0,double i=0.0) {r=r,i=i;} Complex operator + (Complex & A) const {return Complex (R+A.R,I+A.I);} Complex operator-(Complex & A) const {return Complex (R-A.R,I-A.I);} Complex operator * (Complex & A) const {return Complex (R*A.R-I*A.I,R*A.I+I*A.R);}}; #define MAXN 600010#define Pai acos ( -1.0) Complex a[maxn],b[maxn];int len,n,ans[maxn];char s[maxn];inline void Prework () {Len=1;while (Len < (n<<1)) len<<=1; Extended to 2 power for (int i=n; i<len; i++) A[i]=complex (0,0), B[i]=complex (0,0); Complement 0}inline void Rader (Complex *x) {for (int i=1,j=len>>1,k; i<len-1; i++) {if (i<j) swap (x[i],x[j]); K=len >>1;while (j>=k) j-=k,k>>=1;if (j<k) j+=k;} bitwise reverse Permutation inline void DFT (Complex *x,int opt) {rader (x); for (int h=2; h<=len; h<<=1)//Operation length, h=1 no tube {Complex Wn (cos (Opt*2*pai/h), sin (opt*2*pai/h)), for (int i=0; i<len; i+=h) {Complex W (1,0); for (int j=i; j<i+h/2; J + +) {Complex u=x[j],t=w*x[ J+h/2];x[j]=u+t,x[j+h/2]=u-t; Butterfly operation W=w*wn;}}} if (opt==-1)//interpolation to/len for (int i=0; i<len; i++) X[i].r/=len;} Opt=1 DFT opt=-1 IDFT inline void FFT (Complex *a,complex *b) {DFT (a,1); DFT (b,1); for (int i=0; i<len; i++) a[i]=a[i]*b[i]; Point value by DFT (a,-1);} int main () {scanf ("%d", &n), scanf ("%s", s+1), for (int i=n,l=-1; i>=1; i--) a[++l].r=s[i]-' 0 '; scanf ("%s", s+1); (int i=n,l=-1; i>=1; i--) b[++l].r=s[i]-' 0 '; Prework (); FFT (A, B), for (int i=0; i<len; i++) ans[i]= (int) (a[i].r+0.5), for (int i=0; i<len; i++) ans[i+1]+=ans[i]/10,ans[i]%= 10;while (--len &&!ans[len]);//stripping multiply and go 0 for (int i=len; i>=0; i--) putchar (ans[i]+ ' 0 ');p UTS (""); return 0;}
"BZOJ-2179" FFT fast Fourier FFT