Skip to content

Instantly share code, notes, and snippets.

@devteampentagon
Created November 14, 2016 21:19
Show Gist options
  • Select an option

  • Save devteampentagon/322e0de6cd40bdae86c9aca35ce1fda9 to your computer and use it in GitHub Desktop.

Select an option

Save devteampentagon/322e0de6cd40bdae86c9aca35ce1fda9 to your computer and use it in GitHub Desktop.
Extended GCD
#include <bits/stdc++.h>
#define MEM(a,b) memset((a),(b),sizeof(a))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIn(a,b) ((a)<(b)?(a):(b))
#define MIn4(a,b,c,d) MIn(MIn(MIn(a,b),c),d)
#define In freopen("In.txt", "r", stdin);
#define Out freopen("out.txt", "w", stdout);
#define i64 long long
#define u64 long long unsigned
#define sz 1000000
using namespace std;
void extended_euclid(int A,int B)
{
//calculate the value for x and y
// Ax + bY = D
int m,n,p,q,a,b;
int x=1,y=0;
int u=0,v=1;
a = A,b = B;
while(b!=0)
{
m = a%b;
q = a/b;
x = x - (u*q);
y = y - (v*q);
swap(x,u);
swap(y,v);
a = b;
b = m;
}
//printing the result in a format
cout << A << "(" << x << ") + " << B << "(" << y << ") = " << a << endl;
}
int main()
{
int d,x,y,a,b,c;
while(cin >> a >> b)
{
extended_euclid(a,b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment