-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ adds algorithm to find diophantine equation solutions
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
ll gcd(ll a, ll b, ll &x, ll &y) { | ||
if (b == 0) { | ||
x = 1; | ||
y = 0; | ||
return a; | ||
} | ||
ll x1, y1; | ||
ll d = gcd(b, a % b, x1, y1); | ||
x = y1; | ||
y = x1 - y1 * (a / b); | ||
return d; | ||
} | ||
|
||
bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0) { | ||
auto g = gcd(a < 0 ? -a : a, b < 0 ? -b : 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
\subsection{Linear Diophantine Equation: Find any solution} | ||
|
||
Given $a$ $b$, $c$ finds the solution to the equation $ax + by = c$, the result will be stored in the reference variables $x0$ and $y0$ | ||
|
||
time: $O(\log{min(a,b)})$ |