HDU 1402(A * B Problem Plus-FFT速度测试)

来源:互联网 发布:个人自动发卡平台源码 编辑:程序博客网 时间:2024/05/16 06:36

A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15111    Accepted Submission(s): 2942


Problem Description
Calculate A * B.
 

Input
Each line will contain two integers A and B. Process to end of file.

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

Output
For each case, output A * B in one line.
 

Sample Input
1210002
 

Sample Output
22000
 

Author
DOOM III
 

Recommend
DOOM III   |   We have carefully selected several similar problems for you:  1215 1695 1066 1042 1408 
 




#include<bits/stdc++.h>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define pb push_back#define mp make_pair #define eps (1e-1)#define MAXN (200000+10)#define pi ((double)3.1415926535897932384626)typedef long long ll;typedef complex<double> cd;class fft { public:cd A[MAXN];int n,l;void brc(cd *A,int l) {int i,j,k;for(i=1,j=l>>1;i<l-1;i++) {if (i<j) swap(A[i],A[j]);k=l>>1;while(j>=k) {j-=k;k>>=1;} j+=k;} }void DFT(int l,int on) //on {brc(A,l);for(int h=2;h<=l;h<<=1) {cd wn=cd(cos(on*2*pi/h),sin(on*2*pi/h));for(int j=0;j<l;j+=h) {cd w=cd(1,0);for(int k=j;k<j+h/2;k++) {cd u=A[k],t=w*A[k+h/2];A[k]=u+t;A[k+h/2]=u-t;w*=wn;}}}if (on==-1) Rep(i,l) A[i]/=l;//DFT = 逆矩阵=-A/l  }void mem(int _n) {MEM(A)  n=_n;l=1; while(l<n) l<<=1; l<<=1;}void scan(char *a,int n) {MEM(A)Rep(i,n) {A[i]=cd(a[n-i-1]-'0',0);}}}S1,S2;char s1[MAXN],s2[MAXN];int ans[MAXN];int main(){//freopen("hdu1402.in","r",stdin);//freopen(".out","w",stdout);while(scanf("%s%s",s1,s2)==2) {int n=strlen(s1),m=strlen(s2);int N=max(n,m);S1.mem(N),S2.mem(N);S1.scan(s1,n);S2.scan(s2,m);S1.DFT(S1.l,1);S2.DFT(S2.l,1);Rep(i,S1.l) S1.A[i]*=S2.A[i];S1.DFT(S1.l,-1);MEM(ans)Rep(i,S1.l) {ans[i]+=(int)(S1.A[i].real()+0.1);ans[i+1]+=ans[i]/10;ans[i]%=10;} int L=S1.l;while(L&&!ans[L]) --L;RepD (i,L) printf("%d",ans[i]);putchar('\n');}return 0;}




0 0
原创粉丝点击