poj1996

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

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

 

你可能感兴趣的:(poj)