~ "_" "~
Cough cough!!! Write this note today, in case the dementia will not solve the equation later!!!
Begin!
~1~, first of all, saw a gcd (a, B), what the hell is this thing? What kind of thing is not important, the important thing she represents, in fact, GCD (A, A, b) represents a non-negative integer A and a (not 0) of the greatest common divisor, (number theory in general: Calculation of A and B of the maximum common factor of the lower effective method is my daughter's four-year teacher teaching method, the teacher asked the students to find Factor, and then find the largest number that appears in two tables at the same time. yes! A Good idea for elementary school students! ) 。 Now, we can't do that!
All right! Start with gcd (A, B)!
The following are the general formulas:
A = b * q1 + R1;
b = R1 *q2 + R2;
R1 = R2 * Q3 + R3;
R2 = R3 * Q4 + R4;
。。。。。。
。。。。。。
When is this writing going to be the head ~_~ ...
Obviously there must be a place for the end, because there will definitely be a R for 0 (R2 >= R4 >= R6 ...), R1 >= R3 >= R5 ...), OK, keep writing down:
Rn-1 = Rn * qn+1 + rn+1;
Rn = rn+1 * qn+2 + rn+2; Suppose rn+2 = = 0
。。。。。。。。。。。。。 Ok! ^_^ ........
rn+2 = 0, then by the last formula Rn+1 divisible by Rn, then, rn+1 divisible Rn-1,,,, it is clear that this recursion can be pushed to the first formula, So,rn+1 division a,rn+1 divisible by B, that is, Rn+1 is a and B convention number, is not the largest It? First, write rn+1 as G, set D as any of the conventions of A and B, by the first
The formula (a = b * q1 + R1) can be known, D is divided into R1, and then into the second formula, D divide R2, push, push, push, push, and finally know that D evenly divisible by rn+1, that is, d is evenly divisible by G,
Because d is an arbitrary number of conventions, D is divisible by G, then D is greatest common divisor, then G is both the number of conventions and multiples of greatest common divisor, only: g is greatest common divisor.
in front ",,,, recursion. "Hidden a big secret, that is gcd (A, b) = gcd (b,a%b), this secret Ah, very wonderful!" Why do we have this conclusion? From the first formula ( < Span style= "color: #000000; font-size:16px; " > a = b * Q1 + R1 ) Obviously, gcd (A, b) = g; Again obviously gcd (b,r1) = g; Obviously, GCD (A, b) = gcd (b,a%b),
。。。。。。。。。。。 Ok. The problem was solved. The code is attached ....... .....
1 int gcd (int A,int b)2{3 return b==0a:gcd (b,a%b); 4 }
GCD
~2~, Well, in fact, the front is just chatter! But I can bet five cents in front of things no tricky!
The following is a positive solution to this problem: a*x + b*y = gcd (A, b);
The following are the general formulas:
A = b * q1 + R1; R1 = a-q1 * b;
b = R1 *q2 + R2; R2 = b-q2 * R1;
R1 = R2 * Q3 + R3; R3 = r1-q3 * R2;
R2 = R3 * Q4 + R4;
。。。。。。
It is worth noting that the right side of the equation. It can be found that each R can be represented as multiples of A and B (R1 (1,-Q1)), and a struct A, B (OK, two) can be defined.
A.x, A.y respectively represents a, b coefficients, Initializes a = a = (1,0), b = b = (0,1), then R1 = a-q1*b; You can see that the R1 is also used below, and you can't use a.
Just make A = R1 much better! A = a-b*q1; See R2 again, R2 = B-Q2*R1 = B-q2*a; The same makes B = R2; b = b-a*q2; below R3 = A-q3*b;
A = a-q3*b; then: B = b-a*q ... Wait, when does it end? Before "Look at R2 again", consider such a question, if R1 = = g? Obviously this time will be terminated, pseudo code as follows:
while (1) {
Q = A/b;
R = a%b;
A = A-b*q;
(A, b) = (b,a%b);
(A, b) = (b,a)//This makes it easier to write code
if (a%b = = 0) break;
}
: 0 requires special award.
The code is as follows:
1 voidSolintAintBint&g)2 {3a.x = B.Y =1 ;4A.Y = b.x =0 ;5 if(A = =0) {6g =b;7a.x =1;8A.Y =0;9 return ;Ten } One if(b = =0) { Ag =A; -a.x =0; -A.Y =1; the return ; - } - Node C; - intQ, R; + while(1) { -Q = A/b; +r = a%b; Aa.x = a.x-b.x*Q; atA.Y = a.y-b.y*Q; -A =b; -b =R; - if(A%b = =0) Break; -C =A; -A =B; inB =C; - } tog =b; +}
EX_GCD
~3~. Not to be continued.
The solution of the number theory study note linear equation a*x + b*y = gcd (A, B)