-
Couldn't load subscription status.
- Fork 2
Extended Euclidean Algorithm (Solving Diophantine equations)
Khanh Nguyen Vu edited this page May 30, 2020
·
1 revision
ll gcd(ll a, ll b, ll & x, ll & y) {
if (!a) {
x = 0, y = 1;
return b;
}
ll x1, y1;
ll d = gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
bool diophantine(ll a, ll b, ll c, ll & x0, ll & y0, ll & g) {
g = gcd(abs(a), abs(b), x0, y0);
if (c % g) return false;
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}