保存推导部分:
gcd = b*x1 + (a-(a/b)*b)*y1
= b*x1 + a*y1 – (a/b)*b*y1
= a*y1 + b*(x1 – a/b*y1)
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int exgcd(int a, int b, int &x, int &y)
{
if(!b)
{
x = 1; y = 0;
return a;
}
int ans = exgcd(b,a%b,x,y);
int t = x;
x = y;
y = t-a/b*y;
return ans;
}
int main()
{
int a, b, x, y;
cin >> a >> b;
cout << exgcd(a, b, x, y) << endl;
cout << x << " " << y << endl;
return 0;
}