Skip to content

Instantly share code, notes, and snippets.

@tuket
Created September 14, 2017 06:47
Show Gist options
  • Save tuket/02c992d1e9122cce45d6218e61510bc5 to your computer and use it in GitHub Desktop.
Save tuket/02c992d1e9122cce45d6218e61510bc5 to your computer and use it in GitHub Desktop.
gcd
// 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