[uoj#34] Polynomial multiplication
Question Description
This is a template problem.
Give you two polynomial, please output the polynomial after multiplication.
Input
The first line is two integers n and m, each representing the number of two polynomial.
Second linen +1 An integer representing the coefficients before the 0 to N times of the first polynomial, respectively.
Third linem +1 An integer that represents the coefficients before the 0 to M times of the first polynomial, respectively.
Output
Linen +m +1 An integer that represents the coefficients before the 0 to n+m times of the polynomial after multiplication .
Input example
1 2 1 2 1 2 1
Output example
1 4 5 2
Data size and conventions
0 ≤n ,m ≤105 to ensure that the coefficients in the input are greater than or equal to 0 and less than or equal to 9.
Exercises
Paste template.
By the way FFT Learning Link: Picks's blog
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace Std;int read () {int x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if (c = = ') -') f =-1; c = GetChar (); }while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 400010const Double pi = ACOs ( -1.0); int N, m;struct Complex {double A, B; Complex operator + (const complex& t) {Complex ANS;ANS.A = a + t.a;ans.b = B + t.b;return ans;} Complex operator-(const complex& t) {Complex ANS;ANS.A = a-t.a;ans.b = B-t.b;return ans;} Complex operator * (const complex& t) {Complex ANS;ANS.A = a * t.a-b * t.b;ans.b = a * t.b + b * t.a;return ans;} Complex operator *= (const complex& t) {*this = *this * T;return *this;}} A[MAXN], B[maxn];int ord[maxn];void FFT (complex* x, int n, int tp) {for (iNT i = 0; I < n; i++) if (I < ord[i]) swap (X[i], x[ord[i]); for (int i = 1; i < n; I <<= 1) {Complex wn, w; wn.a = cos (pi/i); w N.B = (double) tp * SIN (pi/i), for (int j = 0; J < n; j + = (I << 1)) {W.A = 1.0; w.b = 0.0;for (int k = 0; k < i ; k++) {Complex T1 = x[j+k], t2 = w * x[j+k+i];x[j+k] = t1 + t2;x[j+k+i] = t1-t2;w *= wn;}}} return;} int main () {n = read (); m = read (); for (int i = 0; I <= N; i++) a[i].a = (double) read (), a[i].b = 0.0;for (int i = 0; I & lt;= m; i++) b[i].a = (double) read (), b[i].b = 0.0;int L = 0;m + = n; for (n = 1; n <= m; n <<= 1) l++;for (int i = 0; i < n; i++) ord[i] = (ord[i>>1] >> 1) | ((I & 1) << L-1); FFT (A, n, 1); FFT (b, N, 1), for (int i = 0; I <= N; i++) a[i] *= b[i]; FFT (A, N,-1), for (int i = 0; i < m; i++) printf ("%d", (int) (a[i].a/n +. 5)); printf ("%d\n", (int) (a[m].a/n +. 5)); return 0;}
[uoj#34] Polynomial multiplication