Skip to content

Commit 6218eff

Browse files
committed
extgcd takes int64_t instead of uint64_t
To prevent overflow
1 parent d68f2a8 commit 6218eff

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

aoj/NTL/NTL1E/main-iterative.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#include <bits/stdc++.h>
22

3-
inline uint64_t extgcd(
4-
uint64_t const a,
5-
uint64_t const b,
3+
inline int64_t extgcd(
4+
int64_t const a,
5+
int64_t const b,
66
int64_t &x,
77
int64_t &y
88
)
99
{
1010
x = 1, y = 0;
1111
int64_t x1 = 0, y1 = 1;
12-
uint64_t a1 = a, b1 = b;
12+
int64_t a1 = a, b1 = b;
1313
// a * x + b * y = a1
1414
// a * x1 + b * y1 = b1
1515
while (b1 > 0) {
16-
uint64_t q = a1 / b1;
16+
int64_t q = a1 / b1;
1717
std::tie(x, x1) = std::make_tuple(x1, x - q * x1);
1818
std::tie(y, y1) = std::make_tuple(y1, y - q * y1);
1919
std::tie(a1, b1) = std::make_tuple(b1, a1 - q * b1);

aoj/NTL/NTL1E/main-recursive.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <bits/stdc++.h>
22

3-
inline uint64_t extgcd(
4-
uint64_t const a,
5-
uint64_t const b,
3+
inline int64_t extgcd(
4+
int64_t const a,
5+
int64_t const b,
66
int64_t &x, int64_t &y
77
)
88
{
@@ -11,7 +11,7 @@ inline uint64_t extgcd(
1111
return a;
1212
}
1313
int64_t x1, y1;
14-
uint64_t g = extgcd(b, a % b, x1, y1);
14+
int64_t g = extgcd(b, a % b, x1, y1);
1515
x = y1;
1616
y = x1 - y1 * (a / b);
1717
return g;

lib/cpalgo/algebra/extgcd_iterative.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
// Verified:
1515
// - https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/1/NTL_1_E
1616
//
17-
inline uint64_t extgcd(
18-
uint64_t const a,
19-
uint64_t const b,
17+
inline int64_t extgcd(
18+
int64_t const a,
19+
int64_t const b,
2020
int64_t &x,
2121
int64_t &y
2222
)
2323
{
2424
x = 1, y = 0;
2525
int64_t x1 = 0, y1 = 1;
26-
uint64_t a1 = a, b1 = b;
26+
int64_t a1 = a, b1 = b;
2727
// a * x + b * y = a1
2828
// a * x1 + b * y1 = b1
2929
while (b1 > 0) {
30-
uint64_t q = a1 / b1;
30+
int64_t q = a1 / b1;
3131
std::tie(x, x1) = std::make_tuple(x1, x - q * x1);
3232
std::tie(y, y1) = std::make_tuple(y1, y - q * y1);
3333
std::tie(a1, b1) = std::make_tuple(b1, a1 - q * b1);

lib/cpalgo/algebra/extgcd_recursive.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
// Verified:
2323
// - https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/1/NTL_1_E
2424
//
25-
inline uint64_t extgcd(
26-
uint64_t const a,
27-
uint64_t const b,
25+
inline int64_t extgcd(
26+
int64_t const a,
27+
int64_t const b,
2828
int64_t &x, int64_t &y
2929
)
3030
{
@@ -33,7 +33,7 @@ inline uint64_t extgcd(
3333
return a;
3434
}
3535
int64_t x1, y1;
36-
uint64_t g = extgcd(b, a % b, x1, y1);
36+
int64_t g = extgcd(b, a % b, x1, y1);
3737
x = y1;
3838
y = x1 - y1 * (a / b);
3939
return g;

lib/main/algebra/extgcd/template-extgcd-interop.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <bits/stdc++.h>
44
#include "template/template-main.hpp"
55

6-
void action_extgcd(std::function<uint64_t(uint64_t, uint64_t, int64_t&, int64_t&)> const& extgcd) {
6+
void action_extgcd(std::function<uint64_t(int64_t, int64_t, int64_t&, int64_t&)> const& extgcd) {
77
int64_t a, b, x, y, g;
88
std::cin >> a >> b;
99

@@ -16,7 +16,7 @@ void action_extgcd(std::function<uint64_t(uint64_t, uint64_t, int64_t&, int64_t&
1616
void setup(
1717
std::string& header,
1818
std::map<std::string,Command>& commands,
19-
std::function<uint64_t(uint64_t, uint64_t, int64_t&, int64_t&)> const& extgcd
19+
std::function<uint64_t(int64_t, int64_t, int64_t&, int64_t&)> const& extgcd
2020
) {
2121
commands["extgcd"] =
2222
Command { "extgcd {a} {b}", std::bind(action_extgcd, extgcd) };

0 commit comments

Comments
 (0)