Topics
A linear Diophantine equation is of the form: ax + by = c
, where a, b, c
are integers, and we seek integer solutions x, y
.
A solution exists if and only if gcd(a, b)
divides c
:
- If
gcd(a, b) ∤ c
, there are no solutions - If
gcd(a, b) | c
, there are infinitely many solutions
Note
We use the extended euclidean algorithm to find one particular solution
(x0, y0)
forax + by = gcd(a, b)
and scale it byc/g
bool solve_diophantine(int a, int b, int c, int &x, int &y) {
int g = extended_gcd(abs(a), abs(b), x, y);
if (c % g != 0) return false;
x *= c / g;
y *= c / g;
// Adjust signs if a or b was negative
if (a < 0) x = -x;
if (b < 0) y = -y;
return true; // Solution exists
}
General solution
If is a particular solution, the general solution is:
where is an integer, and g = gcd(a, b)
. This shows that there are infinitely many solutions if gcd(a, b) | c
.
A unique solution exists only if
a
andb
are coprime (gcd(a, b) = 1
) and additional constraints are applied (e.g., boundingx
andy
to a specific range). Otherwise, there are infinitely many solutions.