Skip to content

Commit 841eb05

Browse files
committed
add lazy_exp and newton.hpp
1 parent 780146f commit 841eb05

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cp-algo/math/lazy_convolution.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ namespace cp_algo::math {
6464
cdq(1, n);
6565
return C;
6666
}
67+
68+
template<typename base>
69+
auto lazy_exp(base p1, auto &&get_p, size_t n) {
70+
// Q = exp P
71+
// Q' = Q * P'
72+
auto Qd = lazy_multiply<base>(base(1), p1, [&](auto &Q, auto &Pd, auto &Qd, size_t k) {
73+
auto pdk = get_p(Qd, Pd, k + 1) * base(k + 1);
74+
auto qk = Qd[k - 1] * small_inv<base>(k);
75+
return std::pair{qk, pdk};
76+
}, n);
77+
for(size_t i = n - 1; i >= 1; i--) {
78+
Qd[i] = Qd[i - 1] * small_inv<base>(i);
79+
}
80+
Qd[0] = base(1);
81+
return Qd;
82+
}
6783
}
6884

6985
#endif // CP_ALGO_MATH_LAZY_MULTIPLY_HPP

cp-algo/math/newton.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef CP_ALGO_MATH_NEWTON_HPP
2+
#define CP_ALGO_MATH_NEWTON_HPP
3+
4+
#include "poly.hpp"
5+
6+
namespace cp_algo::math {
7+
template<typename base>
8+
auto newton(auto &&FFd, base f0, size_t n) {
9+
using polyn = poly_t<base>;
10+
polyn f = polyn(f0);
11+
for(size_t len = 1; len < n; len *= 2) {
12+
// f -= F(f) / F'(f)
13+
auto [Ff, Fdf] = FFd(f, 2 * len);
14+
Ff.div_xk_inplace(len);
15+
Ff.mul_truncate(Fdf.inv_inplace(len), len);
16+
f -= Ff.mul_xk_inplace(len);
17+
}
18+
return f.mod_xk_inplace(n);
19+
}
20+
}
21+
22+
#endif // CP_ALGO_MATH_NEWTON_HPP

0 commit comments

Comments
 (0)