Bzoj_2179_fft Fast Fourier _ (FFT)

Source: Internet
Author: User

Describe

http://www.lydsy.com/JudgeOnline/problem.php?id=2179

Super-Large integer multiplication

Analysis

FFT template problem.

Think of numbers as a polynomial, X is 10. Then multiply by FFT, and the final rounding is good.

Attention:

1. To put each bit plus 0.5 (or less) before rounding, and then rounding down, it should be the calculation error of floating point number ...

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn=140000;5 Const DoublePi=acos (-1.0);6 intLen;7 intREV[MAXN],ANS[MAXN];8 CharSTR[MAXN];9 structcp{//plural (complex)Ten     Doubler,i; OnecpDoubler_=0.0,Doublei_=0.0): R (R_), I (I_) {} ACpoperator+ (ConstCP &x)Const{returnCP (r+x.r,i+x.i); } -Cpoperator- (ConstCP &x)Const{returnCP (r-x.r,i-x.i); } -Cpoperator* (ConstCP &x)Const{returnCP (r*x.r-i*x.i,r*x.i+i*X.R); } the }A[MAXN],B[MAXN],A[MAXN]; - voidBrcint&len) {//Binary Reverse substitution (bit-reverse-copy) -memset (rev,-1,sizeofrev); -     intk=1, l=0; +      while(K<len) k<<=1, l++; -len=K; +rev[0]=0; rev[len-1]=len-1; A      for(intI=1; i<len-1; i++){ at         if(rev[i]!=-1)Continue; -         intx=i,y=0, m=l; -          while(m--) y<<=1, y|= (x&1), x>>=1; -Rev[i]=y; rev[y]=i; -     } - } in voidDFT (CP *a,intNintFlag) {//discrete Fourier transform (discrete-fourier-transform) -      for(intI=0; i<n;i++) a[rev[i]]=A[i]; to      for(intI=0; i<n;i++) a[i]=A[i]; +      for(intm=2; m<=n;m<<=1){ -CP wn (COS (2.0*pi/m*flag), Sin (2.0*pi/m*flag)); the          for(intI=0; i<n;i+=m) { *CP W (1.0,0.0);intK=m>>1; $              for(intj=0; j<k;j++){Panax NotoginsengCP T=w*a[i+j+k], u=a[i+j]; -a[i+j]=u+T; thea[i+j+k]=u-T; +w=w*WN; A             } the         } +     } -     if(flag==-1) for(intI=0; i<n;i++) a[i].r/=N; $ } $ voidReadin (CP *a) { -scanf"%s", str); -     intL=strlen (str); the      for(intI=0; i<l;i++) a[i].r=str[l-1-i]-'0'; - }Wuyi intMain () { thescanf"%d",&len); -len=len*2-1; Wu Readin (a); Readin (b); - BRC (len); AboutDFT (A,len,1); DFT (B,len,1); $      for(intI=0; i<len;i++) a[i]=a[i]*B[i]; -DFT (a,len,-1); -      for(intI=0; i<len;i++) ans[i]=a[i].r+0.5; -      for(intI=0; i<len;i++) ans[i+1]+=ans[i]/Ten, ans[i]%=Ten; Alen++; +      while(!ans[len]&&len) len--; the      for(inti=len;i>=0; i--) printf ("%d", Ans[i]); -     return 0; $}
View Code

2179:fft Fast Fourier time limit:10 Sec Memory limit:259 MB
submit:2567 solved:1308
[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

Hintsource

Bzoj_2179_fft Fast Fourier _ (FFT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.