5#include "cplib/conv/conv.hpp"
15 Polynomial(
const std::vector<F> &_coef) : coef(_coef) {}
17 Polynomial(std::vector<F> &&_coef) : coef(std::move(_coef)) {}
19 size_type size()
const {
23 void trim_leading_zeros() {
24 while (!coef.empty() && coef.back() == F(0)) {
29 void resize(size_type new_size) {
30 coef.resize(new_size, F(0));
34 for (size_type i = 0; i < other.size(); i++) {
35 if (i < coef.size()) {
36 coef[i] += other.coef[i];
38 coef.push_back(other.coef[i]);
50 for (size_type i = 0; i < other.size(); i++) {
51 if (i < coef.size()) {
52 coef[i] -= other.coef[i];
54 coef.push_back(-other.coef[i]);
65 void negate_inplace() {
90 ret.coef.reserve(deg);
91 ret.coef.emplace_back(F(1) / coef[0]);
92 size_type ret_size = 1;
94 while (ret_size < deg) {
95 Polynomial prod(vector<F>(coef.begin(), coef.begin() + std::min(ret_size, size())));
97 prod.negate_inplace();
100 ret_size = std::min(ret_size * 2, deg);
101 ret.resize(ret_size);
108 reverse(ret.coef.begin(), ret.coef.end());
113 std::pair<Polynomial, Polynomial> div_mod(
const Polynomial &other)
const {
114 if (size() < other.size()) {
117 size_type quot_size = size() - other.size() + 1;
120 quot = this->rev() * other.rev().fps_inverse(quot_size - 1);
121 quot.resize(quot_size);
122 reverse(quot.coef.begin(), quot.coef.end());
125 quot.coef.emplace_back(0);
129 rem.negate_inplace();
130 quot.coef[0] = rem.coef[other.size() - 1] / other.coef.back();
131 for (size_type i = 0; i < other.size(); i++)
132 rem.coef[i] -= quot.coef[0] * other.coef[i];
133 rem.trim_leading_zeros();
138 return div_mod(other).first;
142 *
this = *
this / other;
146 return div_mod(other).second;
150 *
this = *
this % other;
155 using size_type = std::vector<F>::size_type;
void convolve_inplace(std::vector< T > &a, const std::vector< T > &b)
In-place convolution where one array is modified.
Definition: conv.hpp:85