【数学】简易多项式

简易的多项式。用来粗暴模拟。

struct Poly {
    static const int MAXN = 1e3 + 10;
    int deg, f[MAXN];
    Poly() {
        deg = 0, memset(f, 0, sizeof(f));
    }
    int& operator[](int index) {
        return f[index];
    }
    void maintain() {
        deg = MAXN - 1;
        while (deg && f[deg] == 0)
            --deg;
    }
};

void show(Poly& f) {
    f.maintain();
    printf("%d\n", f.deg);
    for (int i = 0; i <= f.deg; ++i)
        printf("%d%c", f[i], " \n"[i == f.deg]);
}

Poly add(Poly& f, Poly &g) {
    f.maintain(), g.maintain();
    Poly h;
    h.deg = max(f.deg, g.deg);
    for (int i = 0; i <= h.deg; ++i)
        h[i] = f[i] + g[i];
    h.maintain();
    return h;
}

Poly sub(Poly& f, Poly &g) {
    f.maintain(), g.maintain();
    Poly h;
    h.deg = max(f.deg, g.deg);
    for (int i = 0; i <= h.deg; ++i)
        h[i] = f[i] - g[i];
    h.maintain();
    return h;
}

Poly mul(Poly& f, Poly &g) {
    f.maintain(), g.maintain();
    Poly h;
    h.deg = f.deg + g.deg;
    for (int i = 0; i <= f.deg; ++i) {
        for (int j = 0; j <= g.deg; ++j)
            h[i + j] += f[i] * g[j];
    }
    h.maintain();
    return h;
}
上一篇:【模板】vector 封装模意义下的多项式操作


下一篇:ZJOI2020 抽卡