Created
September 14, 2017 06:47
-
-
Save tuket/02c992d1e9122cce45d6218e61510bc5 to your computer and use it in GitHub Desktop.
gcd
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
// public domain | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
int a, b; | |
cin >> a >> b; | |
int A = a; | |
int B = b; | |
vector<int> v; | |
while(a % b != 0) | |
{ | |
int x = a / b; | |
v.push_back(x); | |
x = a % b; | |
a = b; | |
b = x; | |
} | |
int g = b; | |
int r = 1; | |
int s = -v.back(); v.pop_back(); | |
while(!v.empty()) | |
{ | |
int x = v.back(); v.pop_back(); | |
int ps = s; | |
s = r - s * x; | |
r = ps; | |
} | |
cout << g << " = " << r << "*" << A << ((s>=0)?" +":"") << s << "*" << B << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment