HDU 1402 A * B problem Plus (FFT + large number multiplication)

Source: Internet
Author: User

A * B Problem PlusTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 13456 Accepted Submission (s): 2401


Problem descriptioncalculate A * B.

Inputeach line would contain the integers A and B. Process to end of file.

Note:the length of each integer would not exceed 50000.

outputfor, Output A * B in one line.

Sample Input
1210002

Sample Output
22000


After doing this, I have a little sense of the FFT.


#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <cmath    >using namespace Std;const double Pi=acos ( -1.0); const int Maxn=50010;struct complex{Double A, B; Complex () {} Complex (double _a,double _b): A (_a), B (_b) {} Complex operator + (const Complex &c) {return    Complex (A+C.A,B+C.B);    } Complex Operator-(const Complex &c) {return Complex (A-C.A,B-C.B);    } Complex operator * (const Complex &c) {return Complex (A*C.A-B*C.B,A*C.B+B*C.A);    }}num1[maxn*4],num2[maxn*4];string str1,str2;int cnt,ans[maxn*4];void Ready () {cnt=1;    int len1=str1.size (), len2=str2.size ();    while (Cnt<2*len1 | | cnt<2*len2) cnt<<=1;    for (int i=len1-1,j=0;i>=0;i--, j + +) Num1[j]=complex (str1[i]-' 0 ', 0);    for (int i=len1;i<=cnt;i++) Num1[i]=complex (0,0);    for (int i=len2-1,j=0;i>=0;i--, j + +) Num2[j]=complex (str2[i]-' 0 ', 0); for (int i=len2;i<=cnt;i++) Num2[i]=coMplex (0,0);}        void Change (Complex s[],int len) {for (int i=1,j=len/2;i<len-1;i++) {if (i<j) swap (s[i],s[j]);        int K=LEN/2;            while (j>=k) {j-=k;        k/=2;    } if (j<k) j+=k;    }}void FFT (Complex s[],int len,int on) {change (S,len);        for (int i=2;i<=len;i<<=1) {Complex wn=complex (cos (-on*2*pi/i), sin (-on*2*pi/i));            for (int j=0;j<len;j+=i) {Complex W (1,0);                 for (int k=j;k<j+i/2;k++) {Complex u=s[k];                 Complex T=W*S[K+I/2];                 S[k]=u+t;                 S[k+i/2]=u-t;            W=w*wn;    }}} if (On==-1) {for (int i=0;i<len;i++) S[i].a/=len;    }}void deal () {FFT (num1,cnt,1);    FFT (num2,cnt,1);    for (int i=0;i<cnt;i++) num1[i]=num1[i]*num2[i]; FFT (num1,cnt,-1);}    void Solve () {for (int i=0;i<cnt;i++) ans[i]= (int) (num1[i].a+0.5); for (int i=0;i<cnt;i++) {ANS[I+1]=ANS[I+1]+ANS[I]/10;    ans[i]=ans[i]%10;    } cnt=str1.size () +str2.size ()-1;    while (ans[cnt]==0 && cnt>0) cnt--;    for (int i=cnt;i>=0;i--) Putchar (ans[i]+ ' 0 '); Putchar (' \ n ');}          int main () {while (CIN&GT;&GT;STR1&GT;&GT;STR2) {ready ();          Deal ();    Solve (); } return 0;}



HDU 1402 A * B problem Plus (FFT + large number multiplication)

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.