码迷,mamicode.com
首页 > 其他好文 > 详细

UOJ #34. 多项式乘法

时间:2018-02-19 19:18:58      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:event   opera   names   分享   clu   void   gpo   exp   cst   

#34. 多项式乘法

这是一道模板题。

给你两个多项式,请输出乘起来后的多项式。

输入格式

第一行两个整数 nn 和 mm,分别表示两个多项式的次数。

第二行 n+1n+1 个整数,表示第一个多项式的 00 到 nn 次项系数。

第三行 m+1m+1 个整数,表示第二个多项式的 00 到 mm 次项系数。

输出格式

一行 n+m+1n+m+1 个整数,表示乘起来后的多项式的 00 到 n+mn+m 次项系数。

样例一

input

1 2
1 2
1 2 1

output

1 4 5 2

explanation

(1+2x)(1+2x+x2)=1+4x+5x2+2x3(1+2x)⋅(1+2x+x2)=1+4x+5x2+2x3。

限制与约定

0n,m1050≤n,m≤105,保证输入中的系数大于等于 00 且小于等于 99。

时间限制1s1s

空间限制256MB

 

分析

FFT模板题。

code

递归:

技术分享图片
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 const int N = 300100;
 9 const double eps = 1e-8;
10 const double pi = acos(-1.0);
11 typedef long long LL;
12 
13 struct Complex {
14     double x,y;
15     Complex() {x=0,y=0;}
16     Complex(double xx,double yy) {x=xx,y=yy;}
17 
18 }A[N],B[N];
19 
20 Complex operator + (Complex a,Complex b) {
21     return Complex(a.x+b.x,a.y+b.y);
22 }
23 Complex operator - (Complex a,Complex b) { 
24     return Complex(a.x-b.x,a.y-b.y);
25 }
26 Complex operator * (Complex a,Complex b) {
27     return Complex(a.x*b.x-a.y*b.y,a.x*b.y+b.x*a.y);
28 }
29 
30 void FFT(Complex *a,int n,int ty) {
31     if (n==1) return ;
32     Complex a1[n>>1],a2[n>>1];
33     for (int i=0; i<=n; i+=2) {
34         a1[i>>1] = a[i],a2[i>>1] = a[i+1];
35     }
36     FFT(a1,n>>1,ty);
37     FFT(a2,n>>1,ty);
38     Complex w1 = Complex(cos(2.0*pi/n),ty*sin(2.0*pi/n));
39     Complex w = Complex(1.0,0.0);
40     for (int i=0; i<(n>>1); i++) {
41         Complex t = w * a2[i];
42         a[i+(n>>1)] = a1[i] - t;
43         a[i] = a1[i] + t;
44         w = w * w1;
45     }
46 }
47 int main() {
48     int n,m;
49     scanf("%d%d",&n,&m);
50     for (int i=0; i<=n; ++i) scanf("%lf",&A[i].x);
51     for (int i=0; i<=m; ++i) scanf("%lf",&B[i].x);
52     int fn = 1;
53     while (fn <= n+m) fn <<= 1;
54     FFT(A,fn,1);
55     FFT(B,fn,1);
56     for (int i=0; i<=fn; ++i) 
57         A[i] = A[i] * B[i];
58     FFT(A,fn,-1);
59     for (int i=0; i<=n+m; ++i) 
60         printf("%d ",(int)(A[i].x/fn+0.5));
61     return 0;
62 }
View Code

非递归

技术分享图片
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 const int N = 300100;
 9 const double eps = 1e-8;
10 const double pi = acos(-1.0);
11 typedef long long LL;
12 
13 struct Complex {
14     double x,y;
15     Complex() {x=0,y=0;}
16     Complex(double xx,double yy) {x=xx,y=yy;}
17 
18 }A[N],B[N];
19 
20 Complex operator + (Complex a,Complex b) {
21     return Complex(a.x+b.x,a.y+b.y);
22 }
23 Complex operator - (Complex a,Complex b) { 
24     return Complex(a.x-b.x,a.y-b.y);
25 }
26 Complex operator * (Complex a,Complex b) {
27     return Complex(a.x*b.x-a.y*b.y,a.x*b.y+b.x*a.y);
28 }
29 void FFT(Complex a[],int n,int ty) {
30     for (int i=0,j=0; i<n; ++i) {
31         if (i < j) swap(a[i],a[j]);
32         for (int k=n>>1; (j^=k)<k; k>>=1);//-----
33     }
34     for (int s=1; (1<<s)<=n; ++s) {
35         int m = (1 << s);
36         Complex w1 = Complex(cos(2*pi/m),ty*sin(2*pi/m));
37         for (int i=0; i<n; i+=m) {
38             Complex w = Complex(1,0);
39             for (int k=0; k<(m>>1); ++k) {
40                 Complex t = w*a[i+k+(m>>1)];
41                 a[i+k+(m>>1)] = a[i+k] - t;//-----
42                 a[i+k] = a[i+k] + t; //-----
43                 w = w*w1;
44             }
45         }
46     }
47 }
48 int main() {
49     int n,m;
50     scanf("%d%d",&n,&m);
51     for (int i=0; i<=n; ++i) scanf("%lf",&A[i].x);
52     for (int i=0; i<=m; ++i) scanf("%lf",&B[i].x);
53     int fn = 1;
54     while (fn <= n+m) fn <<= 1;
55     FFT(A,fn,1);
56     FFT(B,fn,1);
57     for (int i=0; i<=fn; ++i) 
58         A[i] = A[i] * B[i];
59     FFT(A,fn,-1);
60     for (int i=0; i<=n+m; ++i) 
61         printf("%d ",(int)(A[i].x/fn+0.5));
62     return 0;
63 }
View Code

 

UOJ #34. 多项式乘法

标签:event   opera   names   分享   clu   void   gpo   exp   cst   

原文地址:https://www.cnblogs.com/mjtcn/p/8454231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!