{"id":1763,"date":"2021-04-02T00:44:33","date_gmt":"2021-04-01T16:44:33","guid":{"rendered":"http:\/\/47.101.202.111\/?p=1763"},"modified":"2021-10-04T22:39:26","modified_gmt":"2021-10-04T14:39:26","slug":"algorithm-number-gcd","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=1763","title":{"rendered":"\u516c\u7ea6\u6570\u3001\u516c\u500d\u6570\u3001\u6269\u5c55EUCLID &#8211; \u6570\u8bba\u7b97\u6cd5\u8be6\u6790"},"content":{"rendered":"<h2>\u6700\u5927\u516c\u7ea6\u6570 Greatest Common Divisor<\/h2>\n<p>\u4e24\u975e\u8d1f\u6574\u6570 $\\scriptsize a$ \u548c $\\scriptsize b$ \u7684\u6700\u5927\u516c\u7ea6\u6570\u8bb0\u4e3a $\\scriptsize d=gcd(a,b)$<\/p>\n<pre lang=\"cpp\">\nint gcd(int a, int b) {\n    return b == 0? a: gcd(b, a % b);\n}\n<\/pre>\n<h3>\u8bc1\u660e<\/h3>\n<p>\u6b32\u8bc1 $\\scriptsize gcd(a,b) = gcd(b, a\\mod b)$<br \/>\n\u5373\u8bc1 $\\scriptsize gcd(a,b)$ \u548c $\\scriptsize gcd(b, a\\mod b)$ \u4e92\u4e3a\u56e0\u6570<\/p>\n<p><strong>\u4e00\u3001\u5148\u8bc1 $\\scriptsize gcd(a,b)$ \u662f $\\scriptsize gcd(b, a\\mod b)$ \u56e0\u5b50<\/strong><br \/>\n\u5373\u8bc1 $\\scriptsize gcd(a,b)$ \u662f $\\scriptsize b$ \u548c $\\scriptsize a\\mod b$ \u7684\u516c\u56e0\u5b50<br \/>\n1\uff09$\\scriptsize gcd(a,b)$ \u672c\u8eab\u662f $\\scriptsize b$ \u7684\u56e0\u5b50<br \/>\n2\uff09\u6b64\u8bc1 $\\scriptsize gcd(a,b)$ \u662f $\\scriptsize a\\mod b$ \u7684\u56e0\u5b50\uff1a$\\scriptsize a\\mod b = a &#8211; \\lfloor a \/ b\\rfloor b$ \u4e3a $\\scriptsize a$ \u548c $\\scriptsize b$ \u7684\u7ebf\u6027\u7ec4\u5408\uff0c\u800c\u4e24\u6570\u7684\u516c\u7ea6\u6570\u4e00\u5b9a\u4e3a\u4e24\u6570\u7ebf\u6027\u7ec4\u5408\u7684\u516c\u7ea6\u6570\uff0c\u5219 $\\scriptsize gcd(a,b)$ \u662f $\\scriptsize a\\mod b$ \u7684\u56e0\u5b50<\/p>\n<p><strong>\u4e8c\u3001\u518d\u8bc1 $\\scriptsize gcd(b, a\\mod b)$ \u662f $\\scriptsize gcd(a,b)$ \u56e0\u5b50<\/strong><br \/>\n\u5373\u8bc1 $\\scriptsize gcd(b, a\\mod b)$ \u662f $\\scriptsize a$ \u548c $\\scriptsize b$ \u7684\u516c\u56e0\u5b50<br \/>\n1\uff09$\\scriptsize gcd(b, a\\mod b)$ \u672c\u8eab\u662f $\\scriptsize b$ \u7684\u56e0\u5b50<br \/>\n2\uff09\u6b64\u8bc1 $\\scriptsize gcd(b, a\\mod b)$ \u662f $\\scriptsize a$ \u7684\u56e0\u5b50\uff1a$\\scriptsize a = \\lfloor a \/ b\\rfloor b + a\\mod b$\uff0c\u5219 $\\scriptsize a$ \u662f $\\scriptsize b$ \u548c $\\scriptsize a \\mod b$ \u7684\u7ebf\u6027\u7ec4\u5408\uff0c\u4e24\u6570\u7684\u516c\u7ea6\u6570 $\\scriptsize gcd(b, a\\mod b)$ \u4e00\u5b9a\u4e3a$\\scriptsize b$ \u548c $\\scriptsize a \\mod b$ \u7ebf\u6027\u7ec4\u5408\u7684\u516c\u7ea6\u6570\uff0c\u5f97\u8bc1<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5355\u8c03\u9012\u51cf\uff0c\u76f4\u81f3\u9012\u5f52\u8fb9\u754c $\\scriptsize gcd(d,0) = d$<\/p>\n<h2>\u6700\u5c0f\u516c\u500d\u6570 Least Common Multiple<\/h2>\n<p>\u4e24\u975e\u8d1f\u6574\u6570 $\\scriptsize a$ \u548c $\\scriptsize b$ \u7684\u6700\u5c0f\u516c\u500d\u6570\u8bb0\u4e3a $\\scriptsize lcm(a,b)$<\/p>\n<pre lang=\"cpp\">\nint lcm(int a, int b) {\n    return a \/ gcd(a, b) * b;\n}\n<\/pre>\n<h3>\u8bf4\u660e<\/h3>\n<p>$\\scriptsize a = gcd(a,b) * x$\uff0c$\\scriptsize b = gcd(a,b) * y$<br \/>\n$\\scriptsize x$ \u548c $\\scriptsize y$ \u662f $\\scriptsize a$ \u548c $\\scriptsize b$ \u7684\u6700\u5c0f\u4e92\u8d28\u56e0\u5b50\uff0c\u5426\u5219 $\\scriptsize gcd(a,b)$ \u672a\u8fbe\u5230\u6700\u5927<br \/>\n$\\scriptsize lcm(a,b) = x * y * gcd(a,b)$ \u662f\u540c\u65f6\u5305\u542b\u8fd9\u4e09\u4e2a\u56e0\u5b50\u7684\u6700\u5c0f\u4e58\u79ef\uff0c\u5373\u4e3a\u6700\u5c0f\u516c\u500d\u6570<\/p>\n<p>\u4f8b\u7801\u4e2d\u5148\u7b97\u9664\u6cd5\u518d\u7b97\u4e58\u6cd5\uff0c\u5229\u4e8e\u907f\u514d\u4e58\u6cd5\u6ea2\u51fa<\/p>\n<h2>\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5 Extend Euclid<\/h2>\n<p>\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u7528\u6765\u8ba1\u7b97\u4e8c\u5143\u4e00\u6b21\u65b9\u7a0b $\\scriptsize ax + by = n$ \u7684\u6574\u6570\u89e3<\/p>\n<pre lang=\"cpp\">\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u5927\u516c\u7ea6\u6570 Greatest Common Divisor \u4e24\u975e\u8d1f\u6574\u6570 $\\scriptsize a$ \u548c $ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=1763\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u516c\u7ea6\u6570\u3001\u516c\u500d\u6570\u3001\u6269\u5c55EUCLID &#8211; \u6570\u8bba\u7b97\u6cd5\u8be6\u6790\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1763"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1763"}],"version-history":[{"count":24,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions"}],"predecessor-version":[{"id":1795,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions\/1795"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1763"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}