Skip to content

Commit 93c1f88

Browse files
committed
Improve inv and bisect
1 parent 5195a0b commit 93c1f88

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

cp-algo/math/poly.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,16 @@ namespace cp_algo::math {
546546
}
547547

548548
// Return {P0, P1}, where P(x) = P0(x) + xP1(x)
549-
std::array<poly_t, 2> bisect() const {
550-
std::vector<T> res[2];
551-
res[0].reserve(deg() / 2 + 1);
552-
res[1].reserve(deg() / 2 + 1);
553-
for(int i = 0; i <= deg(); i++) {
549+
std::array<poly_t, 2> bisect(size_t n) const {
550+
std::deque<T> res[2];
551+
for(size_t i = 0; i < n; i++) {
554552
res[i % 2].push_back(a[i]);
555553
}
556554
return {res[0], res[1]};
557555
}
556+
std::array<poly_t, 2> bisect() const {
557+
return bisect(size(a));
558+
}
558559

559560
// Find [x^k] P / Q
560561
static T kth_rec(poly_t P, poly_t Q, int64_t k) {

cp-algo/math/poly/impl/div.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ namespace cp_algo::math::poly::impl {
9191
}
9292
template<typename poly>
9393
poly inv(poly const& p, size_t n) {
94-
auto q = p.mod_xk(n);
9594
if(n == 1) {
96-
return poly(1) / q[0];
95+
return poly(1) / p[0];
9796
}
9897
// Q(-x) = P0(x^2) + xP1(x^2)
99-
auto [q0, q1] = q.negx().bisect();
98+
auto [q0, q1] = p.bisect(n);
99+
q1 *= -1;
100100

101101
int N = fft::com_size((n + 1) / 2, (n + 1) / 2);
102102

0 commit comments

Comments
 (0)