zoukankan      html  css  js  c++  java
  • poj1996

    多项式计算,有点像母函数,注意最后要判断最高次项是否为0。

    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    #define MAX_NUM 105
    
    int a_num, b_num;
    int a[MAX_NUM], b[MAX_NUM];
    int c[MAX_NUM * MAX_NUM];
    int poly[2][MAX_NUM * MAX_NUM];
    int poly_num;
    
    void input()
    {
        scanf("%d%d", &a_num, &b_num);
        for (int i = 0; i <= a_num; i++)
            scanf("%d", &a[i]);
        for (int i = 0; i <= b_num; i++)
            scanf("%d", &b[i]);
    }
    
    void add_c(int poly[], int a)
    {
        for (int i = 0; i <= poly_num; i++)
            c[i] += a * poly[i];
    }
    
    void add_poly(int last[], int next[])
    {
        memset(next, 0, sizeof(int) * MAX_NUM * MAX_NUM);
        for (int i = 0; i <= b_num; i++)
        {
            for (int j = 0; j <= poly_num; j++)
                next[i + j] += b[i] * last[j];
        }
        poly_num += b_num;
    }
    
    void work()
    {
        memset(c, 0, sizeof(c));
        for (int i = 0; i <= b_num; i++)
        {
            poly[1][i] = b[i];
        }
        poly_num = b_num;
        c[0] = a[0];
        add_c(poly[1], a[1]);
        for (int i = 2; i <= a_num; i++)
        {
            add_poly(poly[(i - 1) & 1], poly[i & 1]);
            add_c(poly[i & 1], a[i]);
        }
        while (c[poly_num] == 0)
            poly_num--;
        printf("%d", c[0]);
        for (int i = 1; i <= poly_num; i++)
            printf(" %d", c[i]);
        puts("");
    }
    
    int main()
    {
        int case_num;
        scanf("%d", &case_num);
        for (int i = 0; i < case_num; i++)
        {
            input();
            work();
        }
    }
    View Code
  • 相关阅读:
    TCC
    使用RocketMQ实现分布式事务
    CentOS关机
    使用grub手动引导linux和windows
    CentOS下X Window与命令行界面的切换
    Centos下 为firefox安装flash插件
    tar.xz文件如何解压
    用Linux命令wget进行整站下载
    CentOS关闭火狐浏览器Flash过期提示
    CentOS普通用户添加sudo权限
  • 原文地址:https://www.cnblogs.com/rainydays/p/3325386.html
Copyright © 2011-2022 走看看