Skip to content

Commit cf7821b

Browse files
authored
Merge pull request #1385 from NicholasPatrick/matrix_product_overflow_fix
Fix off-by-one error in a matrix product test.
2 parents 0862cd4 + 9a867f5 commit cf7821b

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

linear_algebra/matrix_product/gen/unsigned_overflow.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
#include <iostream>
42
#include <set>
53
#include <limits>
@@ -9,17 +7,25 @@
97

108
using namespace std;
119

12-
int main() {
10+
int main(int, char* argv[]) {
11+
long long seed = atoll(argv[1]);
12+
1313
int val = MOD - 2;
14+
unsigned long long max_ull = numeric_limits<unsigned long long>::max();
1415

15-
// Find smallest size n where n * val * val > numeric_limits<unsigned long long>::max()
16+
// Find smallest size n where n * val * val > max_ull
1617
int n = 0;
17-
unsigned long long res = 0;
18-
long long val2 = (long long) val * val;
19-
while (res + val2 >= res) // Check if adding val2 to res will overflow
20-
{
21-
res += val2;
22-
++n;
18+
unsigned long long val2 = (unsigned long long) val * val;
19+
if (seed == 0) {
20+
unsigned long long res = 0;
21+
// Check if adding val2 to res will overflow
22+
// this test mistakenly produces the largest n that does not overflow
23+
while (res + val2 > res) {
24+
res += val2;
25+
++n;
26+
}
27+
} else if (seed == 1) {
28+
n = max_ull / val2 + 1;
2329
}
2430

2531
cout << n << ' ' << n << ' ' << n << '\n';

linear_algebra/matrix_product/hash.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@
4040
"small_09.in": "a5b051bd88e93503771c9681352029cb501aba6ff34136ae571ac148075d781d",
4141
"small_09.out": "2b456d5070b924b76d31b85945eb42362918491c0c7e2e8aedb4e3941563acfa",
4242
"unsigned_overflow_00.in": "b30d8bfb1e59cc60670096505b5eda866052fd8e839244263da5f75f108aeb9a",
43-
"unsigned_overflow_00.out": "b776f9584be10a2f16b3fc22111c6123ddf89a4b6b6058e7a9cec320c2b6a6ab"
43+
"unsigned_overflow_00.out": "b776f9584be10a2f16b3fc22111c6123ddf89a4b6b6058e7a9cec320c2b6a6ab",
44+
"unsigned_overflow_01.in": "e772fad71a028698445b585feb8fcc95695841ef4d69181537e94f908700855d",
45+
"unsigned_overflow_01.out": "b5273ee92bfc3cfb2cb42684db4622825ed605b31292a812f269cd6f8b58658d"
4446
}

linear_algebra/matrix_product/info.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ forum = "https://github.com/yosupo06/library-checker-problems/issues/654"
1919
number = 1
2020
[[tests]]
2121
name = "unsigned_overflow.cpp"
22-
number = 1
22+
number = 2
2323

2424
[params]
2525
N_MAX = 1_024

0 commit comments

Comments
 (0)