FFT模板——copied from hzwer

 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 #include<complex>
14 using namespace std;
15 int read(){
16     int xx=0,ff=1;char ch=getchar();
17     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
18     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
19     return xx*ff;
20 }
21 const int maxn=(1<<18)+1;
22 const double PI=acos(-1.0);
23 typedef complex<double> C;
24 int N,M,L,R[maxn];
25 C a[maxn],b[maxn];
26 void FFT(C a[],int arg){
27     for(int i=0;i<N;i++)
28         if(i<R[i])
29             swap(a[i],a[R[i]]);
30     for(int i=1;i<N;i<<=1){
31         C wn(cos(PI/i),arg*sin(PI/i));
32         for(int p=i<<1,j=0;j<N;j+=p){
33             C w(1,0);
34             for(int k=0;k<i;k++,w*=wn){
35                 C x=a[j+k],y=w*a[j+k+i];
36                 a[j+k]=x+y,a[j+k+i]=x-y;
37             }
38         }
39     }
40 }
41 int main(){
42     //freopen("in","r",stdin);
43     N=read(),M=read();
44     for(int i=0;i<=N;i++)
45         a[i]=read();
46     for(int i=0;i<=M;i++)
47         b[i]=read();
48     M+=N;
49     for(N=1;N<=M;N<<=1)
50         L++;
51     for(int i=0;i<N;i++)
52         R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
53     FFT(a,1);FFT(b,1);
54     for(int i=0;i<=N;i++)
55         a[i]*=b[i];
56     FFT(a,-1);
57     for(int i=0;i<=M;i++)
58         printf("%d ",(int)(a[i].real()/N+0.5));
59     return 0;
60 }
View Code

 

FFT还有蝶形数组那部分不懂,emmmm,先这样吧,抄一个黄学长的模板,体验acmer用模板切题的快感。

 

 

hdu1402

A * B Problem Plus

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
1 2 1000 2
 


Sample Output
2 2000
 


Author
DOOM III
 
 
直接上模板改一改,注意清除前导0和进位(错了一次,逃)
 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 #include<complex>
14 using namespace std;
15 int read(){
16     int xx=0,ff=1;char ch=getchar();
17     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
18     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
19     return xx*ff;
20 }
21 const int maxn=(1<<18)+1;
22 const double PI=acos(-1.0);
23 typedef complex<double> C;
24 int N,M,L,R[maxn];
25 C a[maxn],b[maxn];
26 void FFT(C a[],int arg){
27     for(int i=0;i<N;i++)
28         if(i<R[i])
29             swap(a[i],a[R[i]]);
30     for(int i=1;i<N;i<<=1){
31         C wn(cos(PI/i),arg*sin(PI/i));
32         for(int p=i<<1,j=0;j<N;j+=p){
33             C w(1,0);
34             for(int k=0;k<i;k++,w*=wn){
35                 C x=a[j+k],y=w*a[j+k+i];
36                 a[j+k]=x+y,a[j+k+i]=x-y;
37             }
38         }
39     }
40 }
41 char s1[50010],s2[50010];
42 int ans[100010];
43 int main(){
44     //freopen("in","r",stdin);
45     /*N=read(),M=read();
46     for(int i=0;i<=N;i++)
47         a[i]=read();
48     for(int i=0;i<=M;i++)
49         b[i]=read();*/
50     while(scanf("%s",s1)!=EOF){
51         scanf("%s",s2);
52         N=strlen(s1)-1,M=strlen(s2)-1;
53         for(int i=0;i<=N;i++)
54             a[i]=s1[N-i]-'0';
55         for(int i=0;i<=M;i++)
56             b[i]=s2[M-i]-'0';
57         M+=N;
58         for(N=1;N<=M;N<<=1)
59             L++;
60         for(int i=0;i<N;i++)
61             R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
62         FFT(a,1);FFT(b,1);
63         for(int i=0;i<=N;i++)
64             a[i]*=b[i];
65         FFT(a,-1);
66         for(int i=M;i>=0;i--)
67             ans[i]=(int)(a[i].real()/N+0.5);
68         int k;
69         for(k=0;k<=M||ans[k];k++)
70             if(ans[k]>=10)
71                 ans[k+1]+=ans[k]/10,ans[k]%=10;
72         while(!ans[k]&&k>0)
73             k--;
74         for(int i=k;i>=0;i--)
75             printf("%d",ans[i]);
76         puts("");
77         memset(a,0,sizeof(a));
78         memset(b,0,sizeof(b));
79         memset(R,0,sizeof(R));
80         memset(ans,0,sizeof(ans));
81         L=0;
82     }
83     return 0;
84 }
View Code

 

 
 
 
 
 
 
posted @ 2018-01-19 16:33  咸鱼lzh  阅读(276)  评论(0编辑  收藏  举报