UOJ#34. Polynomial Multiplication

This is a template question.

Given two polynomials, please output the multiplied polynomials.
input format

The first line contains two integers nn and mm, which respectively represent the degree of the two polynomials.

The second line is n+1n+1 integers representing the coefficients of the 00th to nnth degree term of the first polynomial.

The third line is m+1m+1 integers representing the coefficients of the second polynomial of degree 00 to mm.
output format

A line of n+m+1n+m+1 integers representing the coefficients of the multiplied polynomials of degree 00 to n+mn+m.
Example one
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.
Restrictions and Conventions

0≤n, m≤1050≤n, m≤105, ensure that the coefficient in the input is greater than or equal to 00 and less than or equal to 99.

Time limit: 1s1s

Space limit: 256MB

Changed the board of KSkun.

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
inline void swap(int &a, int &b){int tmp = a;a = b;b = tmp;}
inline void swap(double &a, double &b){double tmp = a;a = b;b = tmp;}
inline void read(int &x)
{
    x = 0;char ch = getchar(), c = ch;
    while(ch < '0' || ch > '9') c = ch, ch = getchar();
    while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    if(c == '-') x = -x;
}
const int MAXN = 1 << 22;
const double PI = acos(-1);
struct Complex 
{
    double real, imag;
    Complex(double _real, double _imag){real = _real, imag = _imag;}
    Complex(){real = imag = 0;}
    Complex operator+(const Complex &x) const {return Complex(real + x.real, imag + x.imag);}
    Complex operator-(const Complex &x) const {return Complex(real - x.real, imag - x.imag);}
    Complex operator*(const Complex &x) const {return Complex(real * x.real - imag * x.imag, real * x.imag + imag * x.real);}
    Complex& operator*=(const Complex &x) {return *this = (*this) * x;}
};
int n, m, len, rev[MAXN], tmp;
Complex a[MAXN], b[MAXN];
void fft(Complex *arr, int f) 
{
    for(int i = 0; i < n; i++) if(i < rev[i]) std::swap(arr[i], arr[rev[i]]);
    for(int i = 1; i < n; i <<= 1) 
    {
        Complex wn(cos(PI / i), f * sin(PI / i));
        for(int j = 0; j < n; j += i << 1) 
        {
            Complex w(1, 0);
            for(int k = 0; k < i; k++) 
            {
                Complex x = arr[j + k], y = w * arr[j + k + i];
                arr[j + k] = x + y;
                arr[j + k + i] = x - y;
                w *= wn;
            }
        }
    }
}
int main() 
{
    read(n), read(m);
    for(int i = 0; i <= n; i++) read(tmp), a[i].real = tmp;
    for(int i = 0; i <= m; i++) read(tmp), b[i].real = tmp;
    m += n;
    for(n = 1;n <= m;n <<= 1) ++ len;
    for(int i = 0; i < n; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
    fft(a, 1), fft(b, 1);
    for(int i = 0; i <= n; i++) a[i] *= b[i];
    fft(a, -1);
    for(int i = 0; i <= m; i++) printf("%d ", int(a[i].real / n + 0.5));
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325176716&siteId=291194637