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
HINT Source
FFT bare Topic
Concrete See the Guide of calculation
Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 1310727 #definePi 3.141592653589793238468 using namespacestd;9 Charch;Ten intM,N,ANS[MAXN]; One BOOLOK; A structcomp{ - DoubleRea,ima; - voidClear () {rea=ima=0;} theCompoperator+(ConstComp &x) {return(comp) {rea+x.rea,ima+X.ima};} -Compoperator-(ConstComp &x) {return(comp) {rea-x.rea,ima-X.ima};} -Compoperator*(ConstComp &x) {return(comp) {rea*x.rea-ima*x.ima,rea*x.ima+ima*X.rea};} - }a[maxn],b[maxn],c[maxn],tmp[maxn],w,wn; + voidReadint&x) { - for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; + for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); A if(OK) x=-x; at } - voidRead (COMP *a) { - for(inti=m-1; i>=0; i--){ - for(Ch=getchar ();! IsDigit (CH); ch=GetChar ()); -a[i].rea=ch-'0'; - } in } - voidWriteint*a) { to inti; + for(i=n-1; i>=0&&!a[i];i--); - if(i<0) {puts ("0");return;} the for(; i>=0; i--) printf ("%d",(int) a[i]); *Puts""); $ }Panax Notoginseng voidFFT (Comp *a,intStintSizintStepintop) { - if(siz==1)return; theFFT (a,st,siz>>1,step<<1, op), FFT (a,st+step,siz>>1,step<<1, op); + intx=st,x1=st,x2=st+Step; Aw= (comp) {1,0},wn= (comp) {cos (op*2*pi/siz), sin (op*2*pi/siz)}; the for(intI=0;i< (siz>>1); i++,x+=step,x1+= (step<<1), x2+= (step<<1), w=w*WN) +tmp[x]=a[x1]+ (w*a[x2]), tmp[x+ (siz>>1) *step]=a[x1]-(w*a[x2]); - for(inti=st;siz;i+=step,siz--) a[i]=Tmp[i]; $ } $ intMain () { -Read (m), n=1; - while(n< (m<<1)) n<<=1; the Read (a), read (b); -FFT (A,0N1,1), FFT (b,0N1,1);Wuyi for(intI=0; i<n;i++) c[i]=a[i]*B[i]; theFFT (c,0N1,-1); - for(intI=0; i<n;i++) ans[i]= (int) Round (c[i].rea/(1.0*n)); Wu for(intI=0; i<n;i++) ans[i+1]+=ans[i]/Ten, ans[i]%=Ten; - write (ans); AboutSystem"Pause"); $ return 0; -}
FFT Fast Fourier