LOJ #108. polynomial multiplication,
Memory limit: 256 MiB time limit: 1000 ms standard input/output question type: traditional evaluation method: text comparison Uploader: Anonymous Submission submission record statistics discussion test data question description
This is a template question.
Input two polynomials and output the product of these two polynomials.
Input Format
The first line has two integers, n nn and m mm, representing the number of times of two polynomials, respectively.
The second line contains n + 1 n + 1n + 1 integers, representing the coefficients before the 0 00 to n nn of the first polynomial respectively.
The third line is m + 1 m + 1 m + 1 integer, indicating the coefficient before the 0 00 to m mm of the second polynomial respectively.
Output Format
N + m + 1 n + m + 1n + m + 1 integer in a row, the coefficients before 0 00 to n + m n + mn + m of the polynomial after multiplication.
Sample Input
1 21 21 2 1
Sample output
1 4 5 2
Data range and prompt
0 ≤ n, m ≤ 105 0 \ leq n, m \ leq 10 ^ 50 ≤ n, m ≤ 105, ensure that the input coefficient is greater than or equal to 0 00 and less than or equal to 9 99.
Show category labels
Luogu cannot survive.
I have to submit it here.
Implemented recursively
About FFT can see here http://www.cnblogs.com/zwfymqz/p/8244902.html
# Include <iostream> # include <cstdio> # include <cmath> using namespace std; const int MAXN = 2*1e6 + 10; inline int read () {char c = getchar (); int x = 0, f = 1; while (c <'0' | c> '9 ') {if (c = '-') f =-1; c = getchar () ;}while (c> = '0' & c <= '9 ') {x = x * 10 + c-'0'; c = getchar ();} return x * f;} const double Pi = acos (-1.0 ); struct complex {double x, y; complex (double xx = 0, double yy = 0) {x = xx, y = yy ;}} a [MAXN], B [MAXN]; complex operator + (complex a, complex B) {return complex (. x + B. x,. y + B. y);} complex operator-(complex a, complex B) {return complex (. x-b.x,. y-b.y);} complex operator * (complex a, complex B) {return complex (. x * B. x-a.y * B. y,. x * B. y +. y * B. x);} // void fast_fast_tle (int limit, complex * a, int type) {if (limit = 1) return; // only one constant complex a1 [limit> 1], a2 [limit> 1]; for (int I = 0; I <= limit; I + = 2) // a1 [I> 1] = a [I], a2 [I> 1] = a [I + 1] based on the parity of the lower mark; fast_fast_tle (limit> 1, a1, type); fast_fast_tle (limit> 1, a2, type); complex Wn = complex (cos (2.0 * Pi/limit ), type * sin (2.0 * Pi/limit), w = complex (); // Wn is the unit root, w represents the power for (int I = 0; I <(limit> 1); I ++, w = w * Wn) a [I] = a1 [I] + w * a2 [I], a [I + (limit> 1)] = a1 [I]-w * a2 [I]; // use the nature of the Unit Root, O (1) get another part} int main () {int N = read (), M = read (); for (int I = 0; I <= N; I ++) a [I]. x = read (); for (int I = 0; I <= M; I ++) B [I]. x = read (); int limit = 1; while (limit <= N + M) limit <= 1; fast_fast_tle (limit, a, 1); fast_fast_tle (limit, b, 1 ); // The following 1 indicates the type of the transformation to be performed. // 1 indicates the conversion from the coefficient to the point value. //-1 indicates that the conversion from the point value to the coefficient. // Why Is this correct?, you can refer to the derivation process of the c vector for (int I = 0; I <= limit; I ++) a [I] = a [I] * B [I]; fast_fast_tle (limit, a,-1); for (int I = 0; I <= N + M; I ++) printf ("% d", (int) (a [I]. x/limit + 0.5); return 0 ;}