Skip to content

Commit bbe56c9

Browse files
committed
lazy convolution for only 1 unknown
1 parent 9ed5e40 commit bbe56c9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

cp-algo/math/lazy_convolution.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ namespace cp_algo::math {
3636
cdq(1, n);
3737
return C;
3838
}
39+
40+
template<typename base>
41+
auto lazy_multiply(auto &A, base b0, auto &&get_b, size_t n) {
42+
big_vector<base> B = {b0};
43+
big_vector<base> C(n);
44+
C[0] = A[0] * b0;
45+
assert(n <= size(A));
46+
auto cdq = [&](this auto &&cdq, size_t l, size_t r) -> void {
47+
if (r - l == 1) {
48+
auto bl = get_b(B, C, l);
49+
B.push_back(bl);
50+
C[l] += A[l] * B[0] + A[0] * B[l];
51+
return;
52+
}
53+
auto m = (l + r) / 2;
54+
cdq(l, m);
55+
auto A_pref = std::span(A).subspan(0, r - l);
56+
big_vector<base> B_suf(std::from_range, std::span(B).subspan(l, m - l));
57+
convolution_prefix(B_suf, A_pref, r - l);
58+
B_suf.resize(r - l);
59+
for(size_t i = m; i < r; i++) {
60+
C[i] += B_suf[i - l];
61+
}
62+
cdq(m, r);
63+
};
64+
cdq(1, n);
65+
return C;
66+
}
3967
}
4068

4169
#endif // CP_ALGO_MATH_LAZY_MULTIPLY_HPP

0 commit comments

Comments
 (0)