|
@@ -16,17 +16,11 @@ int gcd(int a, int b) {
|
|
|
int lcm(int a, int b) {
|
|
|
return a/gcd(a,b)*b;
|
|
|
}
|
|
|
-// returns d = gcd(a,b); finds x,y such that d = ax + by
|
|
|
+// returns d = gcd(a,b); finds x,y such that d = ax + by,
|
|
|
+// with |x|+|y| minimal.
|
|
|
int extended_euclid(int a, int b, int &x, int &y) {
|
|
|
- int xx = y = 0;
|
|
|
- int yy = x = 1;
|
|
|
- while (b) {
|
|
|
- int q = a/b;
|
|
|
- int t = b; b = a%b; a = t;
|
|
|
- t = xx; xx = x-q*xx; x = t;
|
|
|
- t = yy; yy = y-q*yy; y = t;
|
|
|
- }
|
|
|
- return a;
|
|
|
+ if (!b) { x = 1; y = 0; return a; }
|
|
|
+ else { int d = extended_euclid(b, a%b, y, x); y -= x*(a/b); return d; }
|
|
|
}
|
|
|
// finds all solutions to ax = b (mod n)
|
|
|
VI modular_linear_equation_solver(int a, int b, int n) {
|
|
@@ -79,7 +73,7 @@ void linear_diophantine(int a, int b, int c, int &x, int &y) {
|
|
|
y = (c-a*x)/b;
|
|
|
}
|
|
|
}
|
|
|
-int main() {
|
|
|
+int test() {
|
|
|
// expected: 2
|
|
|
cout << gcd(14, 30) << endl;
|
|
|
// expected: 2 -2 1
|