zoukankan      html  css  js  c++  java
  • [UOJ 0034] 多项式乘法

    #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。

    限制与约定

    0n,m1050≤n,m≤105,保证输入中的系数大于等于 00 且小于等于 99。

    时间限制1s1s

    空间限制256MB

    题解

    FFT&NTT的模板题, 应该不用多说啥了吧OwO

    FFT的讲解Rush了一晚上也没Rush出来OwO先放一波代码水一篇博(逃)

    参考代码

    GitHub

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <iostream>
     6 #include <algorithm>
     7 
     8 const int MAXN=270000;
     9 const int DFT=1;
    10 const int IDFT=-1;
    11 const double PI=acos(-1);
    12 
    13 struct Complex{
    14     double real;
    15     double imag;
    16     Complex(double r=0,double i=0){
    17         this->real=r;
    18         this->imag=i;
    19     }
    20 };
    21 Complex operator+(Complex a,Complex b){
    22     return Complex(a.real+b.real,a.imag+b.imag);
    23 }
    24 Complex operator-(Complex a,Complex b){
    25     return Complex(a.real-b.real,a.imag-b.imag);
    26 }
    27 Complex operator*(Complex a,Complex b){
    28     return Complex(a.real*b.real-a.imag*b.imag,a.real*b.imag+a.imag*b.real);
    29 }
    30 
    31 Complex a[MAXN],b[MAXN],c[MAXN];
    32 
    33 int n;             //Length of a
    34 int m;             //Length of b
    35 int bln=1;         //Binary Length
    36 int bct;           //Bit Count
    37 int len;           //Length Sum
    38 int rev[MAXN];     //Binary Reverse Sort
    39 
    40 void Initialize();
    41 void FFT(Complex*,int,int);
    42 
    43 int main(){
    44     Initialize();
    45     FFT(a,bln,DFT);
    46     FFT(b,bln,DFT);
    47     for(int i=0;i<=bln;i++){
    48         c[i]=a[i]*b[i];
    49     }
    50     FFT(c,bln,IDFT);
    51     for(int i=0;i<=len;i++){
    52         printf("%d ",int(c[i].real/bln+0.5));
    53     }
    54     putchar('
    ');
    55     return 0;
    56 }
    57 
    58 void FFT(Complex* a,int len,int opt){
    59     for(int i=0;i<len;i++)
    60         if(i<rev[i])
    61             std::swap(a[i],a[rev[i]]);
    62     for(int i=1;i<len;i<<=1){
    63         Complex wn=Complex(cos(PI/i),opt*sin(PI/i));
    64         int step=i<<1;
    65         for(int j=0;j<len;j+=step){
    66             Complex w=Complex(1,0);
    67             for(int k=0;k<i;k++,w=w*wn){
    68                 Complex x=a[j+k];
    69                 Complex y=w*a[j+k+i];
    70                 a[j+k]=x+y;
    71                 a[j+k+i]=x-y;
    72             }
    73         }
    74     }
    75 }
    76 
    77 void Initialize(){
    78     scanf("%d%d",&n,&m);
    79     len=n+m;
    80     while(bln<=len){
    81         bct++;
    82         bln<<=1;
    83     }
    84     for(int i=0;i<bln;i++){
    85         rev[i]=(rev[i>>1]>>1)|((i&1)<<(bct-1));
    86     }
    87     for(int i=0;i<=n;i++){
    88         scanf("%lf",&a[i].real);
    89     }
    90     for(int i=0;i<=m;i++){
    91         scanf("%lf",&b[i].real);
    92     }
    93 }
    Backup

    以及日常图包OwO

  • 相关阅读:
    CS229 6.4 Neurons Networks Autoencoders and Sparsity
    CS229 6.3 Neurons Networks Gradient Checking
    【Leetcode】【Easy】Min Stack
    【Leetcode】【Easy】Merge Sorted Array
    【Leetcode】【Easy】ZigZag Conversion
    【Leetcode】【Easy】Valid Palindrome
    【Leetcode】【Easy】Reverse Integer
    【Leetcode】【Easy】Palindrome Number
    【Leetcode】【Easy】Length of Last Word
    【Leetcode】【Easy】Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/rvalue/p/7351654.html
Copyright © 2011-2022 走看看