求解两个正整数的最大公约数(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. nginx产品环境安全配置-主配置文件

    以下配置为产品环境的nginx基于安全和效率的主配置文件,不包含fastcgi相关配置 cat /etc/nginx/nginx.conf user nginx; worker_processes a ...

  2. 基于 Spring Cloud 的微服务架构实践指南(下)

    show me the code and talk to me,做的出来更要说的明白 本文源码,请点击learnSpringCloud 我是布尔bl,你的支持是我分享的动力! 一.引入 上回 基于 S ...

  3. json.dumps() 和 json.loads()

    转载: https://blog.csdn.net/qinglingls/article/details/96476368

  4. python3(四)list tuple

    # !/usr/bin/env python3 # -*- coding: utf-8 -*- # list是一种有序的集合,可以随时添加和删除其中的元素. classmates = ['Michae ...

  5. 双色球的Python实现

    代码如下: red_ball = [] blue_ball = [] count = 0 while count < 6: n = int(input('\033[31mPlease enter ...

  6. Pandownload作者被抓之后

    近日,pandownload作者被抓,可以说是圈内的大事件,被抓之后, Pandownload 已经是打不开,用不了了 就在我为此感到惋惜的时候,竟然有出来个shengdownload 先来一块看看这 ...

  7. Docker-Bridge Network 01 容器间通信

    本小节介绍bridge network模式下,单机上的容器网络拓扑及通信. 1.前言 对于单机上的容器,Docker提供了bridge.host.none三种网络.我们首先介绍经典的bridge模式. ...

  8. Jmeter连接mysql数据库?so easy!!!

    一.确保mysql数据库能够通过Navicat等远程连接工具连接. 注意:一定是确保能使用navicat连接,而不是dos窗口! 比如笔者需要查询ecshop数据库下的ecs_admin_user表, ...

  9. Daily Scrum 1/14/2016

    Zhaoyang & Yandong: Still optimizing the speech input interface Dong & Fuchen: Image asynchr ...

  10. stand up meeting 12/3/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 初始化弹窗的弹出位置并捕捉弹窗区域内的鼠标控制事件,初步解决弹窗的拖拽功能:    6 UWP对控件的支持各种看不懂,属性 ...