@@ -596,10 +596,10 @@ namespace cp_algo::math {
596
596
}
597
597
598
598
// 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) {
600
600
while (k > Q.deg ()) {
601
601
int n = Q.a .size ();
602
- auto [Q0, Q1] = Q.mulx (- 1 ). bisect ();
602
+ auto [Q0, Q1] = Q.bisect ();
603
603
auto [P0, P1] = P.bisect ();
604
604
605
605
int N = fft::com_size ((n + 1 ) / 2 , (n + 1 ) / 2 );
@@ -609,15 +609,18 @@ namespace cp_algo::math {
609
609
auto P0f = fft::dft<T>(P0.a , N);
610
610
auto P1f = fft::dft<T>(P1.a , N);
611
611
612
+ Q = poly_t (Q0f * Q0f) - poly_t (Q1f * Q1f).mul_xk (1 );
612
613
if (k % 2 ) {
613
- P = poly_t (Q0f * P1f) + poly_t (Q1f * P0f);
614
+ P = poly_t (Q0f *= P1f) - poly_t (Q1f *= P0f);
614
615
} 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 );
616
617
}
617
- Q = poly_t (Q0f * Q0f) - poly_t (Q1f * Q1f).mul_xk (1 );
618
618
k /= 2 ;
619
619
}
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);
621
624
}
622
625
623
626
// inverse series mod x^n
0 commit comments