forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextendedEuclid.cpp
32 lines (23 loc) · 900 Bytes
/
extendedEuclid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
// Returns the Bezout's coefficients of the smallest positive linear combination of a and b (Diophantine Equation).
// using the extended Euclidean algorithm. (i.e. gcd(a, b) = s.a + t.b).
// O(gcd(a, b)) = O(log(n))
pair<int, int> extendedEuclid(int a, int b) {
if (b == 0) return { 1, 0 };
pair<int, int> p = extendedEuclid(b, a % b); // the same as gcd
int s = p.first;
int t = p.second;
return { t, s - t * (a / b) };
}
int gcd(int a, int b) { return (!b ? a : gcd(b,a%b)); }
int main(int argc, char** argv){
if(argc != 3){
cerr<<"Usage: extendedEuclid a b.\nWhere a & b are the variables of equation.";
return 1;
}
int a = atoi(argv[1]), b = atoi(argv[2]);
pair<int,int> ans = extendedEuclid(a,b);
printf("%d * %d + %d * %d = %d\n", ans.first, a, ans.second, b, gcd(a,b));
return 0;
}