gcd

内容:

\(gcd(a,b)=gcd(b,a\% b)\)

用途:

这不废话嘛,当然是用来求最大公约数啊

证明:(这还是四月份的时候cdx巨佬给我讲的qwq)

\(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)\)

关于为什么\(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\)互质

Code:

int gcd(int x, int y) {
    return y == 0 ? x : gcd(y, x % y);
}

谢谢收看,祝身体健康!

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务