UOJ #34. 多項式乘法
阿新 • • 發佈:2018-02-19
statement eps img inpu 次數 遞歸 pre data- hide
#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。
限制與約定
0≤n,m≤1050≤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 usingView Codenamespace 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 }
非遞歸
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. 多項式乘法