\(gcd(a,b)=gcd(b,a\% b)\)spa
这不废话嘛,固然是用来求最大公约数啊code
设\(d=gcd(a.b)\)
则有\(a=md,b=nd\), \(\Rightarrow\) \(a-b=(m-n)d\)
\(\because\) \(m\)与\(n\)互质
\(\therefore\) \((m-n)\)与\(n\)互质
\(\therefore\) \(gcd((m-n)d,nd)=d\)即\(gcd(a-b,b)=d\)
\(\therefore\) \(gcd(a,b)=gcd(a-b,b)\)
\(\therefore\) \(gcd(a,b)=gcd(a\%b,b)\)class
关于为何\(m-n\)与\(n\)互质:
证实:假设\(m-n\)不与\(n\)互质,有\(gcd(m-n,n)=p,p\neq 1\)
则\(m-n=xp,n=yp\)
\(\therefore\) \(m=(x+y)p\)
\(\therefore\) \(a=md=(x+y)pd,b=nd=ypd\)
\(\therefore\) \(a,b\)有公约数\(pd\)
$\because $ \(gcd(a,b)=d\)
\(\therefore\) \(p=1\)与假设矛盾
故\(m-n\)与\(n\)互质gc
Code:co
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
谢谢收看,祝身体健康!math