Euclid and Extended Euclid's explanations and examples codeforces 7C

Source: Internet
Author: User
Tags greatest common divisor

Euclidean theorem:

For integer A, a, B, gcd (b, ==GCD, a%b) ==d (greatest common divisor of A and B), also known as the Euclidean method

Prove:

Because A is a multiple of D, B is a multiple of D; so a%d==0;b%d==0;

Set k=a/b;r=a%b; then a=k*b+r;

From the above: r=a-k*b;

Because A and B are multiples of D, so (A-K*B) is a multiple of D, so R is also a multiple of D;

So gcd (A, b) ==gcd (b, a%b) ==d

And why prove gcd (A, b) ==gcd (b, a%b) ==d This formula to be established?

In fact, GCD (A, B) ==gcd (A, a%b) ==d This formula is also possible, because a is a multiple of D, but before the recursion to do one step, is to determine the size of a and B, if a<b, there is no way to recursive or loop to greatest common divisor, then if a <b, the exchange of A and B, after the exchange, because the known a>b, then a%b<a must be established;

Fact finding proves that gcd (A, b) ==gcd (b, a%b) ==d This formula narrows the range of data processed;

Euclidean applications:

Used to greatest common divisor a B.

Code implementation:

GCD (A, B) ==gcd (A, a%b) ==d, also established #include<stdio.h>int main () {    int m, n, R, T;    scanf ("%d%d", &m, &n);    if (m<n)    {        t=m;        M=n;        n=t;    }    while (r=m%n, r!=0)    {        m=m;        n=r;    }    printf ("%d\n", n);    return 0;}

  

GCD (A, B) ==gcd (A, a%b) ==d recursive int gcd (int m, int n) {    return N?GCD (N, m%n): M;}

  

GCD (A, B) ==gcd (A, a%b) ==d recursion
int gcd (int m, int n) { if (m%n==0) return n; else return gcd (n, m%n);}

  

GCD (A, b) ==gcd (b, a%b) ==d Loop while ((r=m%n)!=0) {m=n; N=r;}

Extending Euclid's Law: 

For a nonnegative integer A,B;GCD (A, B) that is not exactly 0, the greatest common divisor of a, B, must have an integer pair of x, Y, satisfying a*x+b*y==gcd (A, b);

Prove:

A*X1+B*Y1=GCD (A, b);

b*x2+ (a%b) *y2=gcd (b, a%b);

Because it is known by Euclid's Theorem: gcd (A, b) ==gcd (b, A%b)

So a*x1+b*y1=b*x2+ (a%b) *y2; Because R=a%b, R =a-k*b so ==>

a*x1+b*y1=b*x2+ (a-k*b) *y2; Because k=a/b; so ==>.

A*x1+b*y1=b*x2+ (A-(A/b) *b) *y2; Expand Get ==>    

A*x1+b*y1=b*x2+a*y2-b* (A/b) *y2; Convert to get ==>

a*x1+b*y1=a*y2+b* (x2+ (A/b) *y2);

Observation on the x1=y2, y1=x2-a/b*y2;

It can be inferred that the x1,y1 is x2,y2 by the x2,y2, and thus the analogy is made by x3,y3.

And when is it going to end? That is, recursion gcd (A, B) in b=0, which means that the value of a is required greatest common divisor

That is gcd (a, 0) at this time by expanding Euclidean law a*x+b*y==gcd (A, b) known a*x+b*y=a;

Solve the x=1, y=0;

This is where the recursion terminates:

Extended Euclidean applications:

As far as I know: solving indefinite equations, such as a*x+b*y=c; Values for x and Y are known for the value of a, B, and C

So the question is, how do you apply the extended Euclidean law to solving indefinite equations?

Can convert a*x+b*y=gcd (A, B) *c/gcd (A, b);

Final conversion to a*x/(C/GCD (A, b)) +b*y/(C/GCD (A, b))=GCD (A, b); Finally, the solution x0,y0 by C/GCD (A, B) is the final result.

X1=X0*C/GCD (A, b);

Y1=Y0*C/GCD (A, b);

Code implementation: Example Description: Http://codeforces.com/problemset/problem/7/C

#include <stdio.h>long long exgcd (long long A, long long B, long long &x, long long &y), int main () {    long Long A, B, C, ans, x, y;    while (scanf ("%lld%lld%lld", &a, &b, &c)!=eof)    {        ans=exgcd (A, B, X, y);        if (c%ans==0)        {            X=-x*c/ans;            Y=-y*c/ans;            printf ("%lld%lld\n", x, y);        }        else            printf (" -1\n");    }    return 0;} Long Long EXGCD (long long A, long long B, long long &x, long long &y) {    if (b==0)    {        x=1;        y=0;        return A;    }    Long Long r=exgcd (b, a%b, x, y), T;    t=x;    x=y;    Y=t-(A/b) *y;    return r;}

But it's just a set of solutions X1,y1

The solution set for x, y corresponds to:

X=X1+B/GCD (A, b) *t;

Y=Y1-B/GCD (A, b) *t;

But I can't prove it, if any great God knows, you can tell me!

Euclid and Extended Euclid's explanations and examples codeforces 7C

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.