Summary of GCD and ex_gcd

Source: Internet
Author: User

Gcd () --- indicates the maximum common number. The common method is Euclidean algorithm.

Ex_gcd () --- Extended Euclidean Algorithm

Definition 1: A and B are two integers not all 0, that is, the maximum common divisor of A and B is the maximum common divisor of A and B, and gcd (A, B) is used).

Definition 2: A and B are two non-0 integers. The smallest of the Public multiples of A and B is the smallest public multiple of A and B, which is expressed by LCM (A, B.

The property between the least common multiple and the maximum common divisor:

1. If a | M, B | M, then lcm (a, B) | M

2. If d | a, d | B, d | gcd (A, B)

3. lcm (a, B) = A/gcd (a, B) * B

4. If M, A, and B are positive integers, then lcm (MA, MB) = M * gcd (A, B)

5. If M is not a 0 integer, A1, A2 ...... If it is a public multiple of an, then lcm (a1, a2 ...... An) | M

6. The integers A and B are decomposed into a = p1 ^ (R1) * P2 ^ (R2 )...... Pn ^ (RN), B = p1 ^ (S1) * P2 ^ (S2 )...... Pn ^ (SN). Here, Pi is a different prime number.

Gcd (a, B) = p1 ^ min (R1, S1) * P2 ^ min (R2, S2 )*...... Pn ^ min (RN, SN)

LCM (a, B) = p1 ^ max (R1, S1) * P2 ^ max (R2, S2 )*...... Pn ^ max (RN, SN)

GCD solution ---- moving phase division

Recursive Solution

int gcd(int m,int n){    if(n==0)    return m;    else    return gcd(n,m%n);}

Iterative Solution

int gcd(int m,int n){    int temp;    if(m<n)    {        swap(m,n);    }    while((temp=m%n)!=0)    {        m=n;        n=temp;    }    return n;}


Question connection: PID = 1, 1222

Assume that the step size is m and the total length is N. Here we calculate their cycle, that is, the minimum public multiple. After this cycle, the points of wolf search must be repeated!

Each time a wolf searches for a vertex, he searches for n vertices. The length is exactly the cycle, indicating that he has not repeatedly searched for n vertices! This is converted to GCD.

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;/*int gcd(int m,int n){    int temp;    if(m<n)    {        swap(m,n);    }    while((temp=m%n)!=0)    {        m=n;        n=temp;    }    return n;}*/int gcd(int m,int n){    if(n==0)    return m;    else    return gcd(n,m%n);}int main(){    int m,n;    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&m,&n);        if(gcd(m,n)==1)        printf("NO\n");        else        printf("YES\n");    }    return 0;}


Question connection: PID = 1, 1108

It is very simple, that is, to obtain the least common multiple. You can use the property 3.

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int gcd(int a,int b){    if(b==0)    return a;    else    return gcd(b,a%b);}int main(){    int a,b;    int k;    while(cin>>a>>b)    {        k=a*b/gcd(a,b);        cout<<k<<endl;    }    return 0;}

Extended Euclidean Algorithm:

Set A> B. 1. Obviously when B = 0, gcd (a, B) =. At this time x = 1, y = 0; 2, AB! If it is set to 0, ax1 + by1 = gcd (a, B); bx2 + (a mod B) y2 = gcd (B, A mod B ); according to the simple Euclidean principle, gcd (a, B) = gcd (B, A mod B); then: ax1 + by1 = bx2 + (a mod B) y2; that is: ax1 + by1 = bx2 + (a-[A/B] * B) y2 = ay2 + bx2-(A/B) * by2; obtained according to the constant theorem: X1 = Y2; y1 = x2-[A/B] * Y2; then we get the Method for Solving X1 and Y1: The values of X1 and Y1 are based on X2 and y2.

Code can be written in a loop.

long long ex_gcd(long long a,long long b,long long &x,long long &y){    if(b==0)    {        x=1;        y=0;        return a;    }    long long r=ex_gcd(b,a%b,x,y);    long long t=x;    x=y;    y=t-a/b*y;    return r;}


Question connection: PID = 1, 4180

Question: Give a score (A/B) to find another score (C/D) {d <B), and satisfy the minimum FABS (a/B-c/d. The minimum AB interaction time is BC + 1 = aD | BC = AD + 1, which is in line with Extended Euclidean. Find two d and compare them.

# Include <iostream> # include <cstdio> using namespace STD; int ex_gcd (int A, int B, Int & X, Int & Y) {If (B = 0) {x = 1; y = 0; return a;} int r = ex_gcd (B, A % B, x, y); int T = x; X = y; y = T-A/B * Y; return r;} int main () {int t; CIN> T; int A, B; int X, Y; while (t --) {scanf ("% d/% d", & A, & B); int GCD = ex_gcd (a, B, x, y ); if (GCD! = 1) {printf ("% d/% d \ n", A/GCD, B/gcd); continue; // It must be written here. Otherwise it will wa} if (a = 1) {printf ("% d/% d \ n", A, B-1); continue ;} int C1 = (-y + a) % A; int C2 = (Y + a) % A; int d1 = (x + B) % B; int D2 = (-x + B) % B; If (d1 <D2) printf ("% d/% d \ n", C2, D2 ); else printf ("% d/% d \ n", C1, D1);} return 0 ;}


Question connection: Id = 1061

Two frogs jumped to step T. the coordinate of A was X + M * t, and that of B was y + N * t: X + M * t-y-n * t = p * l, that is, (n-m) * t + L * P = (x-y)

At this time, finding the smallest T is the minimum integer solution for a homogeneous equation (n-m) * t + L * P = (x-y ).

#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>using namespace std;long long gcd(long long a,long long b){    if(!b)return a;    else return gcd(b,a%b);}void ex_gcd(long long a,long long b,long long &x,long long &y){    if(b==0)    {        x=1;        y=0;        return ;    }    ex_gcd(b,a%b,x,y);    long long t=x;    x=y;    y=t-a/b*y;}int main(){    long long x,y,m,n,l;    long long k1,k2;    long long t;    long long a,b,c,g;    while(cin>>x>>y>>m>>n>>l)    {        a=n-m;        b=l;        c=x-y;        g=gcd(a,b);        if(c%g)        {            cout<<"Impossible"<<endl;            continue;        }        a=a/g;        b=b/g;        c=c/g;        ex_gcd(a,b,k1,k2);        t=c*k1/b;        k1=c*k1-b*t;        if(k1<0)        k1+=b;        cout<<k1<<endl;    }    return 0;}

Extended Euclidean algorithms are widely used, and many algorithms use them, such as the same remainder problem.

Euclidean algorithms are suitable for numbers in the range of _ int64, but they are not suitable for a large number. Here we will introduce an algorithm suitable for finding the maximum public approx, its name is the Stein algorithm, which is an improvement of the Euclidean Algorithm and is suitable for integers that exceed 64 bits. It only has shift and addition operations, and its algorithm IDEA is

Gcd (A, A) = A, gcd (K * A, K * B) = K * gcd (A, B)

When K interacts with B, gcd (K * a, B) = gcd (A, B );

Algorithm steps:

1. If a = B, A or B is the maximum common approx. The algorithm ends.

2. If a = 0 and B are the largest common divisor, the algorithm ends.

3. If B = 0, A is the maximum public approx. The algorithm ends.

4. Set a [1] = A, B [1] = B and c [1] = 1

5. If both a [n] and B [N] are even numbers, a [n + 1] = A [n]/2, B [n + 1] = B [N]/2, c [n + 1] = C [N] * 2

6. If a [n] is an even number and B [N] is not, a [n + 1] = A [n]/2, B [n + 1] = B [N], C [n + 1] = C [N]

7. If B [N] is an even number and a [n] is not, a [n + 1] = A [n], B [n + 1] = B [N]/2, c [n + 1] = C [N]

8. If neither a [n] nor B [N] is an even number, a [n + 1] = ABS (A [n]-B [N])/2, B [n + 1] = min (A [n], B [N]), C [n + 1] = C [N]

9. N ++, go to 1

#include<iostream>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;__int64 Stein(__int64 a,__int64 b){    if(a<b)swap(a,b);    if(b==0)return a;    if((a%2==0)&&(b%2==0))return 2*Stein(a/2,b/2);    if(a%2==0)return Stein(a/2,b);    if(b%2==0)return Stein(a,b/2);    return Stein((a-b)/2,b);}int main(){    __int64 a,b;    while(cin>>a>>b)    {        cout<<Stein(a,b)<<endl;    }    return 0;}

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: 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.