HDU 1402 A * B problem Plus (Fast Fourier transform)

Source: Internet
Author: User

Test instructions: Two numbers multiplied, each number of the length does not exceed 10^5;

Idea: The first question of FFT. By converting the coefficient expression to the point value expression, the complexity is reduced; The guide is a good thing!!!

The expression of the point value is calculated by the DfT, and the inverse DFT converts the point value expression to the coefficient expression, which is the computation interpolation; the complexity is O (n^2);

The FFT adopts the thought of dividing and treating, the odd and even is processed separately, the DfT is optimized, and the complexity is O (NLOGN);

FFT processing, (the point value is extended to 2^n)---(compute point value)---(point multiplication operation), and (calculate interpolation);

Because of the change of the position of the result when the parity is divided, the binary position interchange is needed in Fourier transform.

Each time after the butterfly operation DFT Times multiply by two, then the control layer number;

Each iteration changes the value of W to save the time it takes to calculate the WN from 0 each time through the for loop; The butterfly operation calculates the point value;

Inverse calculation interpolation of DFT;

#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;#definePi ACOs (-1.0)Charstr1[5000100],str2[5000100];intnum[5000100];intlen1,len2,n,m;structcomplex{DoubleR,i;    Complex () {}; Complex (DoubleXDoubley) {R=x;i=y; } Complexoperator+(ConstComplex &t)Const{        returnComplex (r+t.r,i+t.i); } Complexoperator-(ConstComplex &t)Const{        returnComplex (r-t.r,i-t.i); } Complexoperator*(ConstComplex &t)Const{        returnComplex (r*t.r-i*t.i,r*t.i+i*T.R); }}x1[5000100],x2[5000100];voidFFT (Complex y[],intNintrev) {     for(intI=1, j,k,t;i<n;i++){        for(j=0,k=n>>1, t=i;k;k>>=1, t>>=1) j=j<<1|t&1;//Binary position swap       if(i<j) Swap (Y[i],y[j]); }     for(ints=2, ds=1; s<=n;ds=s,s<<=1){//number of control layersComplex wn=complex (cos (rev*2*pi/s), sin (rev*2*PI/S)), W=complex (1,0), T;//Initialize unit complex root and Helix factor       for(intk=0; k<ds;k++,w=w*wn) {//Update Helix Factor          for(inti=k;i<n;i+=R) {T=W*Y[I+DS];//Butterfly Operationy[i+ds]=y[i]-T; Y[i]=y[i]+T; }      }    }    if(rev==-1) for(intI=0; i<n;i++) Y[i].r/=n;//Seeking Inverse}intMain () {inti,j,k,t;  while(SCANF ("%s%s", STR1,STR2)! =EOF) {Len1=strlen (STR1); Len2=strlen (STR2); N=1;  while(N&LT;LEN1+LEN2) n<<=1;  for(i=0; i<len1;i++) X1[i]=complex (str1[len1-i-1]-'0',0);  for(; i<n;i++) X1[i]=complex (0,0);  for(i=0; i<len2;i++) X2[i]=complex (str2[len2-i-1]-'0',0);  for(; i<n;i++) X2[i]=complex (0,0);//length ExtensionFFT (X1,n,1); FFT (X2,n,1);//coefficient expression-to-point value expression         for(i=0; i<n;i++) X1[i]=x1[i]*x2[i];//Point value OperationFFT (x1,n,-1);//interpolate Values         for(i=0, t=0; i<n;i++,t/=Ten){//result Processingt+= (int) (x1[i].r+0.1); Num[i]=t%Ten; }         for(; t;t/=Ten) num[i]=t%Ten;  while(n>1&&!num[n-1]) n--;  for(i=n-1; i>=0; i--) printf ("%d", Num[i]); printf ("\ n"); }    return 0;}

HDU 1402 A * B problem Plus (Fast Fourier transform)

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.