给你\(a,b\),求\(x,y\)使得\(xa+yb=gcd(a,b)\)
假设已经求出了\(x,y\)满足\(xb+y(a\mod b)=gcd(a,b)\)
\[
\begin{align}
xb+y(a\mod b)&=gcd(a,b)\xb+y(a-\lfloor\frac{a}{b}\rfloor b)&=\gcd(a,b)\xb+ya-y\lfloor\frac{a}{b}\rfloor b&=\gcd(a,b)\ya+(x-y\lfloor\frac{a}{b}\rfloor )b&=\gcd(a,b)
\end{align}
\]
时间复杂度:\(O(\log n)\)
代码
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1;
y=0;
return a;
}
ll ab=a/b;
ll c=a-b*ab;
ll d=exgcd(b,c,y,x);
y-=x*ab;
return d;
}