Skip to content

Commit 2fa53a2

Browse files
authored
Merge pull request #31 from atcoder/patch/issue30
fix #30: convolution is broken in edge-cases
2 parents a32a17b + 8250de4 commit 2fa53a2

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

atcoder/convolution.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void butterfly(std::vector<mint>& a) {
3434
ie *= ie;
3535
}
3636
mint now = 1;
37-
for (int i = 0; i < cnt2 - 2; i++) {
37+
for (int i = 0; i <= cnt2 - 2; i++) {
3838
sum_e[i] = es[i] * now;
3939
now *= ies[i];
4040
}
@@ -76,7 +76,7 @@ void butterfly_inv(std::vector<mint>& a) {
7676
ie *= ie;
7777
}
7878
mint now = 1;
79-
for (int i = 0; i < cnt2 - 2; i++) {
79+
for (int i = 0; i <= cnt2 - 2; i++) {
8080
sum_ie[i] = ies[i] * now;
8181
now *= es[i];
8282
}

test/unittest/convolution_test.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <atcoder/convolution>
44
#include <atcoder/modint>
55
#include <random>
6+
#include "../utils/random.hpp"
67

78
using namespace atcoder;
89
using uint = unsigned int;
@@ -353,3 +354,33 @@ TEST(ConvolutionTest, ConvLLBound) {
353354
ASSERT_EQ(a, convolution_ll(a, b));
354355
}
355356
}
357+
358+
// https://github.com/atcoder/ac-library/issues/30
359+
TEST(ConvolutionTest, Conv641) {
360+
// 641 = 128 * 5 + 1
361+
const int MOD = 641;
362+
std::vector<ll> a(64), b(65);
363+
for (int i = 0; i < 64; i++) {
364+
a[i] = randint(0, MOD - 1);
365+
}
366+
for (int i = 0; i < 65; i++) {
367+
b[i] = randint(0, MOD - 1);
368+
}
369+
370+
ASSERT_EQ(conv_naive<MOD>(a, b), convolution<MOD>(a, b));
371+
}
372+
373+
// https://github.com/atcoder/ac-library/issues/30
374+
TEST(ConvolutionTest, Conv18433) {
375+
// 18433 = 2048 * 9 + 1
376+
const int MOD = 18433;
377+
std::vector<ll> a(1024), b(1025);
378+
for (int i = 0; i < 1024; i++) {
379+
a[i] = randint(0, MOD - 1);
380+
}
381+
for (int i = 0; i < 1025; i++) {
382+
b[i] = randint(0, MOD - 1);
383+
}
384+
385+
ASSERT_EQ(conv_naive<MOD>(a, b), convolution<MOD>(a, b));
386+
}

0 commit comments

Comments
 (0)