求解两个正整数的最大公约数(Greatest Common Devisor),可以采用循环进行遍历,不过效率很低。所以引入欧几里得算法(Euclid's algorithm)。

欧几里得算法基于GCD递归引理

对任意非负整数a和任意正整数b,

gcd(a, b) = gcd(b, a mod b)

可以直接写出递归程序:

int GCD(int a, int b)
{
if(0 == b)
return a;
else
return GCD(b, a % b);
}
  • 复杂度分析

    运行时间与递归调用的次数成正比。

时间复杂度\(O(lg b)\),最坏情况下计算次数\(N\le 5log_{10}b\)。

证明:欧几里得算法

  • 扩展欧几里得算法

    可以计算出满足下式的三元组\((d,x,y)\):

\[d = GCD(a, b) = ax + by
\]

int Euclid_extend(int a, int b, int* x, int* y)
{
if(0 == b)
{
*x = 1;
*y = 0;
return a;
}
else
{
int r = Euclid_extend(b,a%b,x,y);
int temp = *x;
*x = *y;
*y = temp - (*y)*(a/b);
return r;
}
}

简单证明:

\(b=0\)是递归基,易得一组解\(x=1,y=0\);

\(b \neq0\)时:

首先递归求解:

\[d'=gcd(b,a\%b)=bx'+(a\%b)y' \ \ \ \ \ \ \ \ \ \ \ \ \ \ (1)
\]

我们知道:

\[d=gcd(a,b)=d'=gcd(b,a\%b)\ \ \ \ \ \ \ \ \ \ \ \ \ (2)
\]

\[a\%b=a-b*\biggl\lfloor a/b \biggr\rfloor\ \ \ \ \ \ \ \ \ \ \ (3)
\]

将(2)(3)式带入(1):

\[d=bx'+(a-b\biggl\lfloor a/b \biggr\rfloor)y'=ay'+b(x'-\biggl\lfloor a/b \biggr\rfloor y')
\]

所以,令\(x=y'\)、\(y=x'-\biggl\lfloor a/b \biggr\rfloor y'\),就可以满足\(d=ax+by\)。

GCD-Euclidean Algorithm的更多相关文章

  1. Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  2. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

  3. 扩展欧几里得算法(extended Euclidean algorithm)的一个常犯错误

    int exGcd(int x,int y,int& a,int& b) //ax+by=gcd(x,y) { ; b=; return x; } int res=exGcd(y,x% ...

  4. 算法:辗转相除法【欧几里德算法(Euclidean algorithm)】

     1.来源     设两数为a.b(a>b),求a和b最大公约数(a,b)的步骤如下:用a除以b,得a÷b=q......r1(0≤r1).若r1=0,则(a,b)=b:若r1≠0,则再用b除以 ...

  5. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  6. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  7. HDU 1787 GCD Again(欧拉函数,水题)

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. hdoj 1787 GCD Again【欧拉函数】

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. HDOJ 1787 GCD Again(欧拉函数)

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  10. Greatest common divisor(gcd)

    欧几里得算法求最大公约数 If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop. If B = 0 then GCD(A,B) ...

随机推荐

  1. Nordic nRF52820超低功耗蓝牙5.2 SoC芯片-低端无线连接方案首选

    nRF52820是功耗超低的低功耗蓝牙 (Bluetooth Low Energy /Bluetooth LE).蓝牙mesh.Thread.Zigbee和2.4 GHz专有低端无线连接解决方案.nR ...

  2. Java8 新特性 Lambda & Stream API

    目录 Lambda & Stream API 1 Lambda表达式 1.1 为什么要使用lambda表达式 1.2 Lambda表达式语法 1.3 函数式接口 1.3.1 什么是函数式接口? ...

  3. mybatis源码分析:启动过程

    mybatis在开发中作为一个ORM框架使用的比较多,所谓ORM指的是Object Relation Mapping,直译过来就是对象关系映射,这个映射指的是java中的对象和数据库中的记录的映射,也 ...

  4. Dubbo 路由机制的实现

    Dubbo 路由机制是在服务间的调用时,通过将服务提供者按照设定的路由规则来决定调用哪一个具体的服务. 路由服务结构 Dubbo 实现路由都是通过实现 RouterFactory 接口.当前版本 du ...

  5. "六号标题"组件:<h6> —— 快应用组件库H-UI

     <import name="h6" src="../Common/ui/h-ui/text/c_h6"></import> < ...

  6. Struts2-学习笔记系列(4)-访问servlet api

    5.1通过actioncontext: public String execute() throws Exception { ActionContext ctx = ActionContext.get ...

  7. 【Selenium03篇】python+selenium实现Web自动化:元素三类等待,多窗口切换,警告框处理,下拉框选择

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第三篇博 ...

  8. Linux中使用netstat命令的基本操作,排查端口号的占用情况

    Linux中netstat命令详解 Netstat是控制台命令,是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接口设备的状态信息.Netstat用于显示与I ...

  9. alg-最长公共子序列

    class Solution { public: std::string LongestCommonSubsequence(const std::string& s1, const std:: ...

  10. MD5中使用16进制

    MD5中使用16进制消息摘要 分类: java_secruity2012-12-28 13:11 719人阅读 评论(0) 收藏 举报 消息摘要 由于数据在计算机中的表示,最终以二进制的形式存在,所以 ...