欧几里得(Euclid)算法及其扩展

Euclid 规则:如果x 和 y 是正整数,且有x≥y,那么gcd(x, y) = gcd(x mod y, y)。

证明:待续

 

function Euclid(a, b)

Input: Two integers a and b with a > 0 and b > 0

Output: gcd(a, b)

 

if b = 0: return a

return Euclid(b, a mod b)

 

引理:对于任意的正整数a 和 b,利用扩展Euclid算法可以求得整数x, y 和 d,使得 gcd(a, b) = d = ax + by 成立。

 

function extend-Euclid(a, b)

Input: Two positive integers a and b with a > 0 and b > 0

Output: Integers x, y, d such that d = gcd(a, b) and ax + by = d

 

if b = 0: return (1, 0, a)

(x', y', d) = extend-Euclid(b, a mod b)

return (y', x'- [a/b]y', d)

注:[]为向下取整

证明:待续

 

相关题目:pku 1061 青蛙的约会 http://acm.pku.edu.cn/JudgeOnline/problem?id=1061

解题报告:待续

posted @ 2010-05-21 00:10  superbin  阅读(799)  评论(0编辑  收藏  举报