zoukankan      html  css  js  c++  java
  • UOJ#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

    把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;
    }
    
  • 相关阅读:
    requests模块获取cookie和禁止重定向
    python 获取当前,上级,上上级路径
    jmeter正则表达提取器以及全局变量
    Linux性能监控软件netdata中文汉化版
    windows下搭建jenkins+allure+pytest
    Flask 重写wtforms验证器异常信息
    redis info 详解
    centos 安装setup命令的方法
    redis 配置
    linux 使用 vim 玩python
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/8983599.html
Copyright © 2011-2022 走看看