File tree Expand file tree Collapse file tree
linear_algebra/matrix_product Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1-
2-
31#include < iostream>
42#include < set>
53#include < limits>
97
108using 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 ' ;
Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments