gcd
Let $\gcd(a,b)=\left( a, b\right)=r$
Then $a=pr, b=qr, a \pmod b=a-kb=pr-kqr \mid r$
$\Rightarrow \gcd(a,b) = \gcd(b, a\%b)$
Then we prove that the gcd is max gcd.(Oh, my poor English.)
There are two ways to prove it.
First Way:
Presume that $r$ is the maxium gcd(a,b), and $r$ is different from $r’$ which is the maxium $\gcd(b, a\% b)$.
It means $r > r’$.
$a=pr, b=qr, a\% b=pr-kqr$
$r\mid b, r\mid (a\% b), r>r’$
so $r$ is maxium $\gcd(b, a\% b)$, it’s against to the condition which we presumed.
so $r$ is the maxium $\gcd(a, b)$ and $\gcd(a, a\% b)$.
Second Way:
Let set $A$ be all factor of $\gcd(a, b)$, and let set $B$ be all factor of $\gcd(b, a\% b)$.
if $d\in A$, $a=pd, b=qd, a\% b=pd-kqd$
so $d\mid (a\% b), d\in B$if $d\in B$, $b=pd, a\% b=qd, a=kqd + pd$
so $d \mid a, d\in A$
It means $A=B$, so $\gcd(a,b)_{max}=\gcd(b, a\% b)_{max}$
1 | def gcd(a, b): |
1 | int gcd(int a, int b) { |
exgcd
1 | def exgcd(a, b): |
1 | int exgcd(int a, int b, int &x, int &y) { |