Application of Extended Euclidean algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

Thank you: http://blog.csdn.net/u014634338/article/details/40210435

The application of the extended Euclidean algorithm mainly has the following three aspects:

(1) solving indefinite equation;

(2) The inverse element of the solution module;

(3) Solving the linear linear equation (congruence);

I. Solving the indefinite equation

for indefinite integer equation pa+qb=c,

1. If c mod gcd (p, q) = 0, the equation has an integer solution, otherwise there is no integer solution.
2. After finding a set of solution p0,q0 p * a+q * b = gcd (p, Q), the other integer solutions of p * A+q * b = gcd (p, Q) are satisfied:
p = p0 + B/GCD (p, q) * t
Q = Q0-A/GCD (p, q) * t (where T is an arbitrary integer)

Thus, a series of solutions of P * A+q * b = gcd (p, q) can be obtained.


3. As for the integer solution of pa+qb=c, simply multiply each solution of P * A+q * b = gcd (P, q) by C/GCD (p, q) .

After finding a set of solutions for P * A+q * b = gcd (A, B) p0,q0, it should be a set of solutions that get p * a+q * b = C

   P1 = p0* (C/GCD (b)), Q1 = q0* (C/GCD (A, b)),

P * A+q * b = Other integer solutions of C are satisfied:

p = p1 + B/GCD (A, b) * tq = Q1-A/GCD (A, b) * t (where T is an arbitrary integer)p, q is all integer solutions of p * A+q * b = c.

  

#include <iostream>#include<stdio.h>using namespacestd;intEXGCD (intAintBint&x,int&y)///Templates{    if(b==0) {x=1; Y=0; returnA; }    intR=EXGCD (b, a%b, x, y); intt=y; Y=x-(A/b) *y; X=T; returnR;}intMain () {intX,y,a=5, b=6, n=3; intans=EXGCD (A, B, X, y); if(n%ans) {///No integer solution printf ("no\n"); }    Else{cout<<a<<"x+"<<b<<"y="<<ans<<"an integer solution to:"<<Endl; cout<<"x="<<x<<" "<<"y="<<y<<" "<<Endl; cout<<"A*X+B*Y=GCD (A, B) the first 10 solutions \ n"; intp=x+b/ans;        ///1. intq=y-a/ans; into=1;  while(1){            if(o==Ten) Break; cout<<"x="<<p<<" "<<"y="<<q<<" "<<Endl; o++; P=x+b/ans*o; Q=y-a/ans*o; } cout<<"A*X+B*Y=N*GCD (A, B) the first 10 solutions \ n"; P= p* (n/ans);        ///2. Q= q* (n/ans); o=1;  while(1){            if(o==Ten) Break; cout<<"x="<<p<<" "<<"y="<<q<<" "<<Endl; o++; P=x+b/ans*o; Q=y-a/ans*N; }        ///finding the smallest integer solution        intt=x*n/ans;        ///3. inttemp=b/ans; T= (t%temp+temp)%temp; ints= (n-a*t)/b; cout<<a<<"x+"<<b<<"y="<<n<<"The xmin integer solution is:"<<Endl; cout<<"x="<<t<<" "<<"y="<<s<<" "<<Endl; }    return 0;}

Second, to seek the multiplication inverse element

If Axb≡1 mod n, then A and B are mutually multiplicative inverses (i.e. (a*b)%n=1).

The extended Euclidean algorithm can be used not only to find the greatest common divisor of two positive integers, but also to determine their inverse if the two positive integers are interdependent. Definition: If the integer B≥1,GCD (A, b) = 1, then A has a modulo b multiplication inverse A-1, so that Axa-1≡1mod B here A-1 is called a modulo b multiplication inverse. Theorem: If you give an integer a>0, b>0, there are two integers m, N makes gcd (A, b) = ma + nb If A and B, then MA + nb=1 (Note: M,n has the opposite sign), that is, am≡1 mod b multiplication Inverse is M, if you find this m, then I find a Multiplication inverse element of modulo b

If A and B are not mutually, then a-mode B does not have the multiplication inverse!

In general, we can find countless sets of solutions to meet the conditions, but generally let you solve the smallest group of solutions, how to do? We solved a special solution x0 so, we use x0% m in fact to get the smallest solution. Why?

Can think like this:

is the general solution of X not x0 + n*t (an integer of n>=0)?

So, in other words, a about m inverse is a about m congruence, then according to the minimum integer principle, there must be a minimum positive integer, it is a on the inverse of M, and the smallest must be between (0, M), and only one, which is good explanation.

Perhaps someone noticed, here, I write the general solution is not x0 + (M/GCD) *t, but think about it, GCD = 1, so write with not write is the same, but, because of the particularity of the problem, sometimes we get the special solution x0 is a negative, and sometimes our m It's also a negative number.

When M is negative, we take the absolute value of M, and when the x0 is negative, the result of his m is still negative (the result of computer calculation is this, although the definition of this is not the case), at this time, we still let x0 abs (m) to take the mold, and then the result plus ABS (m) on the line , so it is not difficult for us to write the following code to solve a multiplication inverse of a number a for another number m:

intCalintAintN) {    intx, y; intgcd (EX_GCD (A, n, x, y)); if(1%gcd!=0)    {        return-1; } x*=1/GCD; N=ABS (n); intans=x%N; if(ans<=0) ans+=N; returnans;}

Application of 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.