-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10104 extended euclid.cpp
53 lines (49 loc) · 1.34 KB
/
10104 extended euclid.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
#define N 200005
#define ll long long
#define llu unsigned long long int
#define pare pair<int,int>
#define mod 1000000007
using namespace std;
ll ct=0,cases,X,Y;
///dir array for Queen int dxq[10] = {-1,-1,-1,1,1,1,0,0};
///dir array for Queen int dyq[10] = {-1,0,1,-1,0,1,-1,1};
///dir array for knight int dxk[10] = {-2,-2,-1,-1,1,1,2,2};
///dir array for knight int dyk[10] = {1,-1,-2,2,-2,2,-1,1};
/*int leap(int year){
if( year%4 == 0 && year%100 == !0 )
return 1;
else if (year%100 == 0 && year%400 == 0)
return 1;
else
return 0;
}*/
ll gcd(ll a,ll b){
if(b == 0)return a;
else{
return gcd(b,a%b);
}
}
ll ext_gcd ( ll A, ll B, ll *X, ll *Y ){
ll x2, y2, x1, y1, x, y, r2, r1, q, r;
x2 = 1; y2 = 0;
x1 = 0; y1 = 1;
for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) {
q = r2 / r1;
r = r2 % r1;
x = x2 - (q * x1);
y = y2 - (q * y1);
}
*X = x2; *Y = y2;
return r2;
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ll a,b,ans1,ans2;
while(scanf("%lld %lld",&a,&b)!=EOF){
ll g = ext_gcd(a,b,&ans1,&ans2);
if(g<1)g = -1*g;
printf("%lld %lld %lld\n",ans1,ans2,g);
}
}