Extended Euclidean algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

Code:

1 Long LongE_GCD (Long LongALong LongBLong Long&x,Long Long&y)2 {3     if(b==0)4     {5x=1, y=0;6         returnA;7     }8     Long LongANS=E_GCD (b,a%b,x,y);9     Long Longtmp=x;Tenx=y; Oney=tmp-a/b*y; A     returnans; -}

1) Extended Euclidean algorithm for Ax+by=c

1 BOOLCalintAintBintCint&x,int&y)2 {3     intD=EXGCD (a,b,x,y);4     if(c%d)5         return false;6     intk=c/D;7X*=k; Y*=k;//only one set of solutions is obtained.8     return true;9}

2) solving the modal linear equation

Ax = b (mod n)

The congruence equation Ax≡b (mod n) has a solution for unknown x, when and only if GCD (a,n) | B. And when the equation has a solution, the equation has a gcd (a,n) solution.

Solving equation ax≡b (mod n) is equivalent to solving equation ax+ ny= B, (x, y is an integer)

Set d= gcd (a,n), if integers x and y, satisfy D= ax+ ny (with extended Euclidean). If d| b, the equation

A * x0+ n y0= D, the equation is multiplied by b/d, (because d|b, so divisible), get a A * x0* b/d+ n y0* b/d= B.
So x= x0* b/d,y= y0* b/d a solution for ax+ ny= B, so x= x0* b/d for ax= B (mod n).

A solution for ax≡b (mod n) is x0= x* (b/d) mod n, and the equation's D solution is xi= (x0+ i* (n/d)) mod n {i= 0 ... d-1}.

Set ans=x* (b/d), s=n/d;

The minimum integer solution for equation ax≡b (mod n) is: (ans%s+s)%s;

Relevant proof:

The

     proving equation has one solution: x0 = X ' (b/d) mod n;
    by  a*x0 = A*x ' (b/d) (mod n)
         a*x0 = d (b/ d) (mod n)    (due to ax ' = d (mod n))
                  = B (mod n)

The proof equation has a D solution: Xi = x0 + i* (n/d) (mod n);
by A*xi (mod n) = A * (x0 + i* (n/d)) (mod n)
= (a*x0+a*i* (n/d)) (mod n)
= A * x0 (mod n) (due to D | a)
= b

Let's look at a simple example:

5x=4 (mod3)

Solution x = 2,5,8,11,14 ....

Thus a rule is found, that is, the interval of the solution is 3.

So how is the interval of the solution determined?

If the first solution can be found and the interval between the solutions can be obtained, then the solution set of the linear equation of the modulo is obtained.

We set the interval between the solutions to DX.

So there are

A*x = B (mod n);

A * (X+DX) = B (mod n);

Two-type subtraction, get:

A*DX (mod n) = 0;

That is to say, A*DX is a multiple, but also a multiple of n, that is, A*DX is a and n common multiple. In order to find the DX, we should find a and n least common multiple, at this time the corresponding DX is the smallest.

The greatest common divisor for A and n are D, so the least common multiple of A and n is (a*n)/d.

That is A*DX = a*n/d;

So dx = n/d.

So the interval between the solutions is calculated.

1 BOOLMod_equation (intAintBintN)2 {3     intx,y,x;4     intD=E_GCD (a,n,x,y);5     if(b%d)return false;6x=x* (B/D)%N;7      for(intI=1; i<d;i++) printf ("%d\n", (x+i* (n/d))%n);8     return true;9}

Partial content Replication Source: http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html

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.