Euclidean algorithm and extended Euclidean algorithm (the second problem of the river noip2005 raising group)

Source: Internet
Author: User
Tags gcd greatest common divisor

Euclidean algorithm: Also known as the Euclidean method

GCD (A, B) =gcd (B,A%B);

termination conditions A=GCD b=0;

(GCD is a, B greatest common divisor)

Extended Euclidean algorithm: A and B greatest common divisor is gcd, must be able to find such x and Y, making: a*x + b*y = GCD established

We only need to find special solution x0,y0;

The general solution is X = x0 + (B/GCD) *t y = y0– (A/GCD) *t

So how do we find the next set of solutions?

Modeled after Euclid algorithm a=b,b=a%b.

A%b = A-(A/b) *b (Here the "/" refers to the divide) can be obtained:

GCD = B*x1 + (A-(A/b) *b) *y1

= b*x1 + a*y1– (A/b) *b*y1

= A*y1 + b* (x1–a/b*y1)

Compare easily to x = y1

y = x1–a/b*y1

a*1 + b*0 = gcd is a coefficient of 1,b with a factor of 0 or other value (*0=0)

We'll see the river again. A=s,b=s+1,ax+by=n A, B, greatest common divisor is 1 because it is an adjacent natural number

The ax+by=n is obtained by the Ax+by=1 (1 for greatest common divisor) de coefficient *n.

Not to be continued.

Euclidean algorithm and extended Euclidean algorithm (the second problem of the river noip2005 raising group)

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.