diff --git a/algorithms/math/extended_euclidean.cpp b/algorithms/math/extended_euclidean.cpp index cddc37e2edff9ec4306f9ed7a96fdee98b0f448f..f1422ebfd759696186396859d6bbf6f699c1e34c 100644 --- a/algorithms/math/extended_euclidean.cpp +++ b/algorithms/math/extended_euclidean.cpp @@ -1,6 +1,6 @@ /// Extended Euclidean algorithm /// -/// Time: O(log n) +/// Time: O(log min(a,b)) /// Space: O(1) struct ExtendedEuclidean { diff --git a/algorithms/math/linear_diophantine_equation.cpp b/algorithms/math/linear_diophantine_equation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cdee518550071df799aa9751b45c6eca87d541f --- /dev/null +++ b/algorithms/math/linear_diophantine_equation.cpp @@ -0,0 +1,44 @@ +/// Linear Diophantine Equation +/// +/// Description: +/// A Linear Diophantine Equation is an equation in the form $ax + by = c$. +/// A solution of this equation is a pair $(x,y)$ that satisfies the equation. +/// The locus of (lattice) points whose coordinates $x$ and $y$ satisfy the +/// equation is a straigh line. \par +/// The equation has a solution only if $gcd(a,b) | c$. In the case of +/// existing a solution for the provided $a, b, c$, the infinite set of +/// coordinates $(x,y)$ can be obtained with get(t) for +/// $t=...,-2,-1,0,1,2,...$. +/// +/// Time: O(log min(a,b)) +/// Space: O(1) + +struct Diophantine { + int a, b, c, d; + int x0, y0; + + bool has_solution; + + Diophantine(int a, int b, int c) : + a(a), b(b), c(c) + { init(); } + + void init() { + ExtendedEuclidean ext_gcd; + + int w0, z0; + d = ext_gcd.run(a, b, w0, z0); + if (c % d == 0) { + x0 = w0 * (c / d); + y0 = z0 * (c / d); + has_solution = true; + } else { + has_solution = false; + } + } + + ii get(int t) { + if (!has_solution) return ii(inf, inf); + return ii(x0 + t * (b / d), y0 - t * (a / d)); + } +}; diff --git a/caderno.pdf b/caderno.pdf index dec18373d328149d7ddcb627daf79ae6b63ceb54..ec51993b8af67878947811daeb57ae4959d95f1e 100644 Binary files a/caderno.pdf and b/caderno.pdf differ