"bzoj2179" FFT fast fourier FFT template

Source: Internet
Author: User

2016-06-01 09:34:54

Long, long time ago, I wrote it.

Today, compared to the efficiency, it seems that the handwritten plural is much faster.

Paste the Template:

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <queue>8#include <complex>9 #definell Long LongTen #defineN 500020 One using namespacestd; A intRead () { -   intx=0, f=1;CharCh=GetChar (); -    while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} the    while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} -   returnx*F; - } - structcd{ +   Doubleb; -CD (Doublex=0,Doubley=0) {a=x;b=y;} +Friend CDoperator+ (CD n1,cd n2) {returnCD (n1.a+n2.a,n1.b+n2.b);} AFriend CDoperator-(CD n1,cd n2) {returnCD (n1.a-n2.a,n1.b-n2.b);} atFriend CDoperator* (CD n1,cd n2) {returnCD (n1.a*n2.a-n1.b*n2.b,n1.a*n2.b+n1.b*n2.a);} - }; - intn,m,bit=0; - Const Long DoublePi=acos (-1.0); -   - voidFFT (CD *a,intNinttype) { in    for(intI=0, j=0; i<n;i++) { -     if(j>i) swap (a[i],a[j]); to     intk=N; +      while(j& (k >>=1)) j&=~K; -j|=K; the   } *    for(intI=1; i<=bit;i++){ $CD w_n (cos (2*type*pi/(1<<i)), Sin (2*type*pi/(1<<i)));Panax Notoginseng      for(intj=0;j< (1<<bit); j+= (1<<i)) { -CD W (1,0); the        for(intk=j;k<j+ (1<< (I-1)); k++){ +CD tmp=a[k],tt=w*a[k+ (1<< (I-1))]; Aa[k]=a[k]+tt; thea[k+ (1<< (I-1))]=tmp-tt; +w=w*W_n; -       } $     } $   } -   if(type<0) for(intI=0;i< (1<<bit) (i++) a[i].a= (a[i].a+0.5)/(1<<bit);  - } the   - CD Poly1[n],poly2[n];Wuyi intc[n*2]; the   - CharCh[n]; Wu intMain () { -n=read (); Aboutscanf"%s", ch+1); $    for(intI=0; i<n;i++) poly1[i]= (Double) (ch[n-i]-'0'); -scanf"%s", ch+1); -    for(intI=0; i<n;i++) poly2[i]= (Double) (ch[n-i]-'0'); -bit=1; A    while(1<<bit< (n<<1)) bit++; +n=1<<bit; theFFT (Poly1,n,1); FFT (Poly2,n,1); -    for(intI=0; i<n;i++) poly1[i]=poly1[i]*Poly2[i]; $FFT (poly1,n,-1); the   intjin=0, top=0; the    for(intI=0; i<n;i++){ thejin+= (int) (poly1[i].a+0.5); thec[++top]=jin%Ten; -Jin/=Ten; in   } the    while(top&&c[top]==0) top--; the    while(top) Putchar (c[top--]+'0'); About   return 0; the}
View Code

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

"bzoj2179" FFT fast fourier FFT template

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.