#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll extgcd(ll a,ll b,ll &x,ll &y){
ll d = a;
if(b != 0){
d = extgcd(b,a%b,y,x);
y -= (a/b)*x;
}
else{
x = 1;
y = 0;
}
return d;
}
int main(void)
{
ll a,b,x,y;
cin >> a >> b;
ll GCd = extgcd(a,b,x,y);
if(GCd == 1)//对于ax+by = 1,只有当gcd(a,b) == 1时才有解。
cout << x << y;
else
cout << "NO";
return 0;
}
这里还有另外一种:
ll x,y;
ll extgcd(ll a,ll b){
ll d = a;
if(b != 0){
d = extgcd(b,a%b);
ll t = x;
x = y;
y = t - (a/b)*x;
}else{
x = 1;y = 0;
}
return d;
}
两种都是拓展欧几里得,不过表达不同。