Extended Euclidean algorithm

Source: Internet
Author: User
Tags gcd

Other Good blogs: http://www.cnblogs.com/yefeng1627/archive/2012/12/24/2830594.html

http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html#2985941

The extended Euclidean algorithm is used to solve a set of p,q in known a, b to make P * a+q * b = GCD (P, Q) (the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations. The following is an implementation that uses C + +:
int exgcd (int a, int b, int &x, int &y)
{
if (b = = 0)
{
x = 1;
y = 0;
return A;
}
int r = EXGCD (b, a% B, x, y);
int t = x;
x = y;
y = t-a/b * y;
return R;
}
Comparing this implementation with the recursive implementation of GCD, we find that the following X, Y assignment process is found, which is the essence of extending Euclid's algorithm.
Can think like this:
For a ' = B, b ' = a% B, we get x, Y makes a ' x + b ' y = Gcd (a ', B ')
Because b ' = a% b = a-a/b * B (Note: This is a division in the programming language)
Then you can get:
A ' x + b ' y = Gcd (a ', B ') ===>
BX + (A-A/b * b) y = GCD (a ', b ') = GCD (A, b) ===>
Ay +b (x-a/b*y) = GCD (A, B)
So for A and B, their relative p,q are Y and (x-a/b*y) respectively.
Add: About using the extended Euclidean algorithm to solve the indefinite equation
For the indefinite integer equation pa+qb=c, if C mod Gcd (A, B) = 0, the equation has an integer solution, otherwise there is no integer solution.
A method of finding an integer solution has been listed above, and after finding a set of solutions p0,q0 p * a+q * b = GCD (A, B), the other integer solutions of p * A+q * b = GCD (A, C) are satisfied:
p = p0 + B/GCD (A, b) * t
Q = Q0-A/GCD (A, b) * t (where T is an arbitrary integer)
As for the integer solution of Pa+qb=c, it is only possible to multiply each solution of P * A+q * b = GCD (A, B) by C/GCD (A, B).

转:首先扩展欧几里德主要是用来与求解线性方程相关的问题,所以我们从一个线性方程开始分析。现在假设这个线性方程为a*x+b*y=m,如果这个线性方程有解,那么一定有gcd(a,b) | m,即a,b的最大公约数能够整除m(m%gcd(a,b)==0)。证明很简单,由于a%gcd(a,b)==b%gcd(a,b)==0,所以a*x+b*y肯定能够整除gcd(a,b),如果线性方程成立,那么就可以用m代替a*x+b*y,从而得到上面的结论,利用上面的结论就可以用来判断一个线性方程是否有解。      那么在a*x+b*y=m这个线性方程成立的情况下,如何来求解x和y呢?      1.令a1=a/gcd(a,b),b1=b/gcd(a,b),m1=m/gcd(a,b)。如果我们能够首先求出满足a1*x1+b1*y1=gcd(a1,b1)这个方程的x1和y1,那么x=x1*m1,y=y1*m1就可以求出来了。由欧几里德算法gcd(a,b)=gcd(b,a%b),所以a*x1+b*y1=gcd(a,b)=gcd(b,a%b)=b*x2+(a%b)*y2,现在只要做一些变形就可以得到扩展欧几里德算法中的用到的式子了。令k=a/b(商),r=a%b(余数),那么a=k*b+r。所以r=a-k*b,带入上式,得到a*x1+b*y1=b*x2+(a-(a/b)*b)y2=a*y2+b*(x2-(a/b)*y2) => x1=y2,y1=x2-(a/b)*y2。有了这两个式子我们就知道了在用欧几里德求最大公约数的时候,相应的参数x,y的变化。现在再回过头来看一下扩展欧几里德算法的代码就很好理解了,实际上扩展欧几里德就是在求a和b的最大公约数的同时,也将满足方程a*x1+b*y1=gcd(a,b)的一组x1和y1的值求了出来。下面代码中突出的部分就是标准的欧几里德算法的代码。__int64exGcd(__int64a,__int64b,__int64 &x,__int64&y){    if(b==0){        x=1;        y=0;        returna;    }    __int64g=exGcd(b,a%b,x,y);    __int64temp=x;    x=y;    y=temp-(a/b)*y;    returng;}     2.那么x,y的一组解就是x1*m1,y1*m1,但是由于满足方程的解无穷多个,在实际的解题中一般都会去求解x或是y的最小正数的值。以求x为例,又该如何求解呢?还是从方程入手,现在的x,y已经满足a*x+b*y=m,那么a*(x+n*b)+b*(y-n*a)=m显然也是成立的。可以得出x+n*b(n=…,-2,-1,0,1,2,…)就是方程的所有x解的集合,由于每一个x都肯定有一个y和其对应,所以在求解x的时候可以不考虑y的取值。取k使得x+k*b>0,x的最小正数值就应该是(x+k*b)%b,但是这个值真的是最小的吗??如果我们将方程最有两边同时除以gcd(a,b),则方程变为a1*x+b1*y=m1,同上面的分析可知,此时的最小值应该为(x+k*b1)%b1,由于b1<=b,所以这个值一定会小于等于之前的值。在实际的求解过程中一般都是用while(x<0)x+=b1来使得为正的条件满足,为了更快的退出循环,可以将b1改为b(b是b1的倍数),并将b乘以一个倍数后再加到x上。

Extended Euclidean algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.