Skip to content

Commit 9feb795

Browse files
committed
Bring back mul_slow
1 parent 7799304 commit 9feb795

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

cp-algo/math/fft.hpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,21 @@ namespace cp_algo::math::fft {
263263
point operator [](int i) const {return A.get(i);}
264264
};
265265

266+
void mul_slow(auto &a, auto const& b, size_t k) {
267+
if(empty(a) || empty(b)) {
268+
a.clear();
269+
} else {
270+
int n = std::min(k, size(a));
271+
int m = std::min(k, size(b));
272+
a.resize(k);
273+
for(int j = k - 1; j >= 0; j--) {
274+
a[j] *= b[0];
275+
for(int i = std::max(j - n, 0) + 1; i < std::min(j + 1, m); i++) {
276+
a[j] += a[j - i] * b[i];
277+
}
278+
}
279+
}
280+
}
266281
size_t com_size(size_t as, size_t bs) {
267282
if(!as || !bs) {
268283
return 0;
@@ -271,18 +286,16 @@ namespace cp_algo::math::fft {
271286
}
272287
void mul_truncate(auto &a, auto const& b, size_t k) {
273288
using base = std::decay_t<decltype(a[0])>;
274-
if(size(b) == 0) {
275-
a.clear();
289+
if(std::min({k, size(a), size(b)}) < 64) {
290+
mul_slow(a, b, k);
276291
return;
277292
}
278293
auto n = std::max(flen, std::bit_ceil(
279294
std::min(k, size(a)) + std::min(k, size(b)) - 1
280295
) / 2);
281-
auto A = dft<base>(std::views::take(a, k), n);
282-
if(size(a) != k) {
283-
a.resize(k);
284-
}
285-
if(a == b) {
296+
a.resize(k);
297+
auto A = dft<base>(a, n);
298+
if(&a == &b) {
286299
A.mul(dft<base>(A), a, k);
287300
} else {
288301
A.mul(dft<base>(std::views::take(b, k), n), a, k);

verify/poly/convolution107.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void solve() {
1818
copy_n(istream_iterator<base>(cin), n, begin(a));
1919
copy_n(istream_iterator<base>(cin), m, begin(b));
2020
fft::mul(a, b);
21-
ranges::copy(views::take(a, n + m - 1), ostream_iterator<base>(cout, " "));
21+
ranges::copy(a, ostream_iterator<base>(cout, " "));
2222
}
2323

2424
signed main() {

0 commit comments

Comments
 (0)