How to use extended Euclidean algorithm to solve the modular linear Equation Group (detailed)

Source: Internet
Author: User
Tags gcd greatest common divisor

the a≡b (mod n) is called A and b about modulo n congruence, and the necessary and sufficient condition is that a-a is an integer multiple of n, that is, A-B=ZN (where z takes an integer).

while the modular linear equations ax≡b (mod n) can be written as AX-B=ZN (where z takes an integer), the moving term can be

Ax-zn=b, also known as the form of a two-yuan Ax+by=c equation, using the extended Euclidean algorithm (EXTGCD) can solve the equation whether there is a solution and a set of solutions, and can be based on the group to write a solution system, and then find a special solution, such as the smallest positive integer solution.

The procedure for expanding Euclid's algorithm is given below.

1typedefLong LongLL;2 voidEXTGCD (ll A, ll B, LL &d, LL &x0, LL &y0)3 {4     if(b = =0){5d=a;x=1; y=0;6     }7     Else{8EXTGCD (b,a%b,d,y,x);9Y-= x* (A/b); Ten     } One}

where A, b is a coefficient, D is a greatest common divisor of a, B, and X0,y0 represents a set of special solutions.

by gcd (A, b) = gcd (b,a%b)

ax0 + by0 = gcd (A, b) = gcd (b, a%b) = Bx1 + (a%b) y1

where a%b can also be written a-a/b*b,

so again =bx1 + (a-a/b*b) y1

finishing Ay1 + B (x1-a/b*y1)

X0=y1,y0= (x1-a/b*y1), which is equal to the coefficients , then the x0 and y0 are only required for X1 and Y1, so the x1,y1 is recursive to the next, so the recursion always seeks gcd (AN, 0) = an*xn + 0 * yn , so that xn=1,yn=0 can satisfy the formula, because AN*1+0*0=AN=GCD (an, 0). The x0,y0 is then handed back until it is obtained, and D is the greatest common divisor of a and B.

Here's how to tell if there is an integer solution.

Divide The ax+by=c both sides by D to get

A/D*X+B/D*Y=C/D, because D is a and b of the greatest common divisor, so A/d and b/d are integers, it is required x and y integer solution, C/D must be an integer, otherwise no integer solution.

in conclusion. : Set a,b,c as any integer, D=GCD (A, B), D=ax+by, if C is a multiple of D, there is an integer solution, otherwise there is no integer solution.

The following is how to find a solution system based on a set of solutions (x0,y0), and then find a set of special solutions.

First Use brought into The extension Euclid obtains a solution x0,y0 of AX+B*Y=GCD (A, b);

Observe two more formulas:

Ax+b*y=c 0)

A*x0+b*y0=d 1) ( make d=gcd (A, B))

Divide the 0) both sides by D to:

X* (A/D) +y* (b/d) =c/d

the 1) is multiplied by the b/d and the 0)-type comparison:

(X0*C/D) *a + (Y0*C/D) *b=c

A*x+b*y=c 0)

to x=x0*c/d,y=y0*c/d;

and then refine the above conclusions: Set a,b,c to be any integer, D=GCD (A, b), the equation D=ax+by a set of solutions is (X0,Y0), when C is a multiple of D, then the group of equations Ax+by=c a set of solutions is (X0*C/D,Y0*C/D), when C is not a multiple of D no integer solution.
connect down will A solution expands into a solution system:

       1) divided by D: x0* (A/D) +y0* (b/d) =1

because (A/D) and (b/d) coprime, write the equation:

       (x0+u* (B/D)) * (A/D) + (y0+v* (A/D)) * (b/d) =1 i.e. just u* (b/d) * (A/D)  + v* (A/d) * (B/D) =0 can be;
so x, y  x= (x0+u*) * (A/d)

Y= (y0+v* (A/D)) * (B/D)

Next 'll be Ask The smallest positive integer that satisfies the x of the equation .

so xmin= (x0* (C/D)) mod (b/d)

The specific example is POJ's 1061 frog dating

Https://www.cnblogs.com/wenzhixin/p/9343420.html

How to use extended Euclidean algorithm to solve the modular linear Equation Group (detailed)

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.