ax+by=gcd(a,b) solution
simple derivation
Suppose that $ax_{1}+by_{1}=gcd(a,b)$ is established.
From the Euclidean algorithm, it is known that $gcd(a,b)=gcd(b,a$%$b)$
and $ bx_{2}+(a$%$b)y_{2}=gcd(b,a$%$b)$
combined to get $ax_{1}+by_{1}=bx_{2}+(a$% $b)y_{2}$
$a$%$b=a- \lfloor a/b \rfloor b $
into the above formula to get $ax_{1}+by_{1}=bx_{2}+(a- \lfloor a/b \rfloor b )y_{2}$
is sorted to get $ax_{1}+by_{1}=ay_{2}+b*(x_{2}- \lfloor a/b \rfloor y_{2})$
So finally
\begin{cases}
x_{1}=y_{2}\quad \
y_{1}=x_{2}-\lfloor a/b \rfloor y_{2}\quad
\end{cases}
Now that we have such a recursive relationship, what about the boundaries?
Obviously $gcd(a,0)=a$
, that is, when $b=0$, there are $x=1, y=0$ so that $a 1+b 0=gcd(a,b)$ is established,
then as long as $b When ==0$, return $x=1, y=0$ to get the solution of the original equation step by step
Code
void exgcd(int a,int b,int &x,int &y)
{
if(b==0){x=1;y=0;return a;}
int gcd=exgcd(b,a%b,x,y);
int tp=x;
x=y; y=tp-a/b*y;
return gcd;
}
expand
The above code only finds one set of solutions to the equation, but in fact the answer is far more than one set.
How to find the remaining solutions?
The current solution is $x_{1}, y_{1}$, and the next set of solutions is $x_{1}+s_{1}, y_{1}+s_{2}$
, then there is $a (x_{1}+s_{1})+b(y_{1}+s_{2})=gcd(a,b)$
and $ax_{1}+by_{1}=gcd(a,b) $ simplified
$as_{1}=-bs_{2}$
$\frac {s_{1}}{s_{2}}=-\frac ba=-\frac {b/gcd}{a/gcd}$
In order to make $s_{1}, s_{2}$ the smallest value (in order to get the smallest positive period),
it is obvious that the right side of the equation can be divided by $gcd(a, b)$
In this way, we get the relations of all solutions
\begin{cases}
x_{2}=x_{1}+\frac {b}{gcd(a,b)} k\quad \
y_{2}=y_{1 }-\frac {a}{gcd(a,b)} k\quad
\end{cases}
where k is any integer
In particular, the relation of the minimum non-negative integer solution
of the original equation is $x=(x_{1}$%$\frac {b}{gcd(a,b)}+\frac {b}{gcd(a, b)})$%$\frac {b}{gcd(a,b)}$
****************************
ax+by=c solution
simple derivation
(The following abbreviation $gcd(a,b)$ is $gcd$)
Suppose that $ax_{1}+by_{1}=gcd$ holds
Multiply both sides of the equation by $\frac {c}{gcd}$
to have $a\frac {cx_{1}}{gcd}+b\frac {cy_{1}}{gcd}=c$
So I can first use the Euclidean algorithm to find $x_{1},y_{1}$
then $x=\frac {cx_{1}}{gcd},y=\frac {cy_{1} }{gcd}$ is a set of solutions to the original equation
However, the necessary condition for the existence of a solution of such an equation is $c|gcd(a,b)$
In the same way as above, derive the relations of all solutions
\begin{cases}
x_{2}=\frac {cx_{1}}{gcd}+\frac {b}{gcd(a,b)} k \quad \
y_{2}=\frac {cy_{1}}{gcd}-\frac {a}{gcd(a,b)} k\quad
\end{cases}
where k is any integer
The relation of the minimum non-negative integer solution of the original equation is
$x=(\frac {cx_{1}}{gcd}$%$\frac {b}{gcd(a,b)}+\frac {b}{ gcd(a,b)})$%$\frac {b}{gcd(a,b)}$
************************ **
Solving the congruence ax $\equiv$ c(mod b)
simple derivation
From $ax\equiv c(mod b)$ it is not difficult to get
$(ax-c)$%$b=0$
That is, there must be an integer y such that $ax+by=c$ is established,
and the problem is transformed into the previous model
However, since it is a congruence solution , in fact many solutions
obtained after transforming the equation are the same in the sense of membrane b, so we need to find different solutions in the sense of all membrane b.
Observe the relation of the previous model
\begin{cases}
x_{2}=\frac {cx_{1}}{gcd}+\frac {b}{gcd(a,b)} k\quad \
y_{2} =\frac {cy_{1}}{gcd}-\frac {a}{gcd(a,b)} k\quad
\end{cases}
We find that k takes $[0,gcd(a,b)-1 ]$,
there are exactly gcd(a,b) different solutions in the sense of membrane b