Skip to content

Commit 32f6841

Browse files
committed
Better linrec_single
1 parent b1e54b2 commit 32f6841

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

cp-algo/math/poly.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,10 @@ namespace cp_algo::math {
596596
}
597597

598598
// Find [x^k] P / Q
599-
static T kth_rec(poly_t P, poly_t Q, int64_t k) {
599+
static T kth_rec_inplace(poly_t &P, poly_t &Q, int64_t k) {
600600
while(k > Q.deg()) {
601601
int n = Q.a.size();
602-
auto [Q0, Q1] = Q.mulx(-1).bisect();
602+
auto [Q0, Q1] = Q.bisect();
603603
auto [P0, P1] = P.bisect();
604604

605605
int N = fft::com_size((n + 1) / 2, (n + 1) / 2);
@@ -609,15 +609,18 @@ namespace cp_algo::math {
609609
auto P0f = fft::dft<T>(P0.a, N);
610610
auto P1f = fft::dft<T>(P1.a, N);
611611

612+
Q = poly_t(Q0f * Q0f) - poly_t(Q1f * Q1f).mul_xk(1);
612613
if(k % 2) {
613-
P = poly_t(Q0f * P1f) + poly_t(Q1f * P0f);
614+
P = poly_t(Q0f *= P1f) - poly_t(Q1f *= P0f);
614615
} else {
615-
P = poly_t(Q0f * P0f) + poly_t(Q1f * P1f).mul_xk(1);
616+
P = poly_t(Q0f *= P0f) - poly_t(Q1f *= P1f).mul_xk(1);
616617
}
617-
Q = poly_t(Q0f * Q0f) - poly_t(Q1f * Q1f).mul_xk(1);
618618
k /= 2;
619619
}
620-
return (P * Q.inv(Q.deg() + 1))[k];
620+
return (P *= Q.inv_inplace(Q.deg() + 1))[k];
621+
}
622+
static T kth_rec(poly_t const& P, poly_t const& Q, int64_t k) {
623+
return kth_rec_inplace(poly_t(P), poly_t(Q), k);
621624
}
622625

623626
// inverse series mod x^n

verify/poly/linrec_single.test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ void solve() {
1717
vector<base> a(d), c(d);
1818
copy_n(istream_iterator<base>(cin), d, begin(a));
1919
copy_n(istream_iterator<base>(cin), d, begin(c));
20-
polyn Q = polyn(1) - polyn(c).mul_xk(1);
21-
polyn P = (polyn(a) * Q).mod_xk(d);
22-
cout << polyn::kth_rec(P, Q, k) << endl;
20+
polyn Q = polyn(1) - polyn(c).mul_xk_inplace(1);
21+
polyn P = (polyn(a) * Q).mod_xk_inplace(d);
22+
cout << polyn::kth_rec_inplace(P, Q, k) << endl;
2323
}
2424

2525
signed main() {

0 commit comments

Comments
 (0)