Solution to greatest common divisor-Euclidean algorithm and its expansion (Juchenchu equation)

Source: Internet
Author: User
Tags gcd greatest common divisor integer division

The most famous of the greatest common divisor is the Euclidean rolling division method, which has two forms (recursion and non-recursion, in fact, any recursion can be written as non-recursive), the following to see the implementation of GCD code:

/*** a B greatest common divisor ***/
LongLonggcdLongLongALongLongb) {
if(b = =0)
returnA
Else
returnGCD (b, a% B);
}evidence Process (excerpt from Wikipedia:en. ) Wikipedia. org/wiki/could conversion method )

Let say

Want to prove

First


Available and known.
Indicates that D is the common cause of b,r, but
So



Available and known.
Indicates that e is the public reason for a, B, but
So

To be informed

The extended Euclidean algorithm is an integer solution such as a * x + b * y = (A, b), which can be modeled after the Euclidean algorithm. The procedure is as follows:

/*** extended Euclidean algorithm a*x + b*y = GCD (A, b) of a set of integer solutions, the result exists in X, y ***/
voidEXTEND_GCD (Long LongA,Long LongB,Longlong& x,Long Long&y) {
        if(b = =0) {
x =1;
y =0;
                return;
        }
EXTEND_GCD (b, a% B, x, y);
        Long Longtmp = x;
x = y;
y = tmp-a/b * y;
}

The above procedure only obtains a set of solutions, it is obvious that the solution is not unique: x increases B, y reduces a must be a set of solutions to the original equation:

A * (x + b) + b * (y-a) = a * x + b * y = (A, b).

However, in application, it is often not so simple that the indefinite equation A * x + b * y = n will be solved many times. This is the time to apply the above algorithm:

    1. (A, B), set C = (A, b), if! C|n, there is no integer solution. Because the left and right sides are divided by the C, you can know that the left is an integer, and a non-integer on the other, so the contradiction.
    2. Divide the left and right sides at the same time by C, set the new equation to a ' * x + b ' * y = n ', apply the above algorithm to the solution of a ' * x + b ' * y = 1 (by the first step know (a ', B ') = 1). Set the result to X ', Y '.
    3. x = X ' * n ', y = y ' * n ' is the equation A * x + b * y = n. This is a good understanding, will be a ' * x + b ' * y = 12 edges simultaneously expand n ' Times on the line.
    4. x = X ' * n ' + t * b, y = y ' * n '-T * A (T is an integer) is the original equation A * x + b * y = n all solutions.
linear congruence Equation implementation code:
/* Extended Euclidean theorem extended Euclidean theorem: for two not all 0 integers a, b, there must be a set of solution x, Y, making ax+by==gcd (A, b);    Expand Euclid to achieve the following side of the program, X and y with global variables to save int gcd (int a,int b) {int t,d;        if (b==0) {x=1; y=0; At this time b==0, that is to say gcd (a,0) ==a. The original becomes AX+BY==A=GCD (A, B)-and x==1,y==0 return A; Returns the value of a, B greatest common divisor} d=gcd (b,a%b);    Euclid to seek greatest common divisor application t=x;    X=y;  Y=t-(A/b) *y;     Unknown place 2 return D; Returns the value of a, B greatest common divisor}//Unknown 2 explanation ax+by==gcd (A, B) (1) Description rule, X, Y represents the value at the first recursion, X1,y1 represents the value at the second recursion. Then GCD (A, B) ==gcd (B,A%B), at the same time, are substituting 1, there is ax+by==b*x1+ (a%b) *y1. Transform the right side of the b*x1+ (a%b) *y1==b*x1+ (A-(A/b) *b) *y1==a*y1+b* (x1-(A/b) *y1) and eventually get Ax+by==a*y1+b* (x1-(A/b) *y1) that is:    The x of the previous depth equals the y1 of the next depth, and y of the previous depth is equal to the x1-(A/b) *y1 of the next depth.    * It is important to note that the division used in the derivation above is integer division. Therefore, a set of solutions of the indefinite ax+by==gcd (A, B) are obtained, X, Y. So what should be the solution to the general infinitive ax+by==c? It is very simple, x1=x* (A, B), y1=y* (A, b), a little further, the solution of such a group of solutions is generally not solve any problem, we now have to get all the solution, then what is the solution? Suppose D=gcd (A, B). So x=x0+b/d*t;  Y=y0-a/d*t, where T is an arbitrarily constant integer.  */#include <stdio.h> #include <stdlib.h>/* the minimum positive integer solution for ax≡1 (mod b) of the congruence equation for x. That is to ask ax=mb+R 1=nb+r Deformation ax+ (n-m) b=1, this equation expands Euclid's application ax+by=gcd (A, B), (n-m equivalent to Y) in fact ax≡1 (b) the necessary condition for the solution is gcd (A, a, and A/b), that is, gcd (A, b) = 1; use expand Euclid It is known that Ax+by=1 (x, y is an integer) */int extragcd (int a,int b,int *x,int *y) {int d,t;if (b==0)//recursive call termination condition, when the remainder is 0 stop {*x=1;*y=0, according to Euclid's Euclidean rule ; return A;}     ELSE{D=EXTRAGCD (b,a%b,x,y); t=*x; Based on the value of the next x1,y1, reverse the previous x, Y value *x=*y;*y=t-a/b* (*y); return D;}} int main () {int a,b,x,y;int ans;freopen ("mod.in", "R", stdin), Freopen ("Mod.out", "w", stdout), scanf ("%d%d", &a, &AMP;B); ans=extragcd (a,b,&x,&y); if (ans!=1) return 0;//according to if X is a solution to the equation, then all the solutions of the equation are x+k*b k for integer x=x%b;    guarantees the smallest positive integer solution x, and x belongs to {0,1,2,3...b-1} while (x<=0) x+=b;    printf ("%d\n", X);  return 0;}





Solution to greatest common divisor-Euclidean algorithm and its expansion (Juchenchu equation)

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.