ACM Learning process -51nod1028 Large number multiplication V2 (FFT)

Source: Internet
Author: User

Title Link:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028

The main idea is to ask for a multiplication of two large numbers.

But with the ordinary multiplication of large numbers, the large number of this length is certainly not.

The large number can represent the point value notation, and then convolution multiplication can be used to accelerate the operation of the FFT.

The problem is to save the template.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Longusing namespacestd;//Polynomial multiplication Operations//Fast Fourier transform//FFT//for the convolution of two polynomial, but with a loss of precision//complexity of Time NlognConst intMAXN =400005;Const DoublePI = ACOs (-1.0);structcomplex{DoubleR, I; Complex (DoubleRR =0.0,DoubleII =0.0) {R=RR; I=II; } Complexoperator+(ConstComplex &x) {returnComplex (R+X.R, i+x.i); } Complexoperator-(ConstComplex &x) {returnComplex (R-X.R, I-x.i); } Complexoperator*(ConstComplex &x) {returnComplex (R*X.R-I*X.I, i*x.r+r*x.i); }};//Reid algorithm--inverted position sequence//rader algorithm//perform FFT and ifft before the reverse transformation. //position I and (I binary reversal post position) interchangevoidRader (Complex y[],intLen) {    intj = len>>1;  for(inti =1; I < len-1; i++)    {        if(I <j) Swap (Y[i], y[j]); intK = Len >>1;  while(J >=k) {J-=K; K>>=1; }        if(J < k) J + =K; }}//FFT Implementation//Len must be in the form of 2^k,//On==1 is dft,on==-1 when it is IDFTvoidFFT (Complex y[],intLenintOn )    {rader (y, Len);  for(inth =2; H <= Len; h<<=1)//DFT for calculating length h after divided treatment{Complex wn (cos (-on*2*pi/h), sin (-on*2*pi/h));//The unit complex root e^ (2*pi/m) is unfolded by Euler formula         for(intj =0; J < Len; J + =h) {Complex W (1,0);//rotation Factor             for(intK = J; K < j+h/2; k++) {Complex u=Y[k]; Complex T= w*y[k+h/2]; Y[K]= U+t;//Butterfly Merge Operationy[k+h/2] = uT; W= W*wn;//Update rotation factor            }        }    }    if(On =-1)         for(inti =0; i < Len; i++) Y[I].R/=Len;}//To find convolutionvoidConv (Complex a[], Complex b[],intAns[],intLen) {FFT (A, Len,1); FFT (b, Len,1);  for(inti =0; i < Len; i++) A[i]= a[i]*B[i]; FFT (A, Len,-1); //Precision Recovery     for(inti =0; i < Len; i++) Ans[i]= a[i].r+0.5;}//in-process recovery//for large number multiplicationvoidTurnintAns[],intLenintunit) {     for(inti =0; i < Len; i++) {ans[i+1] + = ans[i]/Unit; Ans[i]%=Unit; }}CharSTR1[MAXN], STR2[MAXN]; Complex ZA[MAXN],ZB[MAXN];intANS[MAXN];intLen;voidInitCharStr1[],Charstr2[]) {    intLen1 =strlen (STR1); intLen2 =strlen (STR2); Len=1;  while(Len <2*len1 | | Len <2*LEN2) Len <<=1; inti;  for(i =0; i < len1; i++) {ZA[I].R= str1[len1-i-1]-'0'; ZA[I].I=0.0; }     while(I <Len) {ZA[I].R= ZA[I].I =0.0; I++; }     for(i =0; i < len2; i++) {ZB[I].R= str2[len2-i-1]-'0'; ZB[I].I=0.0; }     while(I <Len) {ZB[I].R= ZB[I].I =0.0; I++; }}voidWork () {Conv (za, zb, ans, Len); Turn (ans, Len,Ten);  while(ans[len-1] ==0) len--;  for(inti = len-1; I >=0; i--) printf ("%d", Ans[i]); printf ("\ n");}intMain () {//freopen ("test.in", "R", stdin);     while(SCANF ("%s%s", STR1, str2)! =EOF)        {init (str1, str2);    Work (); }    return 0;}
View Code

ACM Learning process -51nod1028 Large number multiplication V2 (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.