Gcd & Exgcd study notes

Euclidean algorithm:

\[gcd(a,b)=gcd(b,a\bmod b)\]

prove:

Obviously (fog)

Extended Euclid and prove:

In order to solve such a shape

\[ax+by=c\]

Equations.

According to Shu Pei theorem, if and only if

\[gcd(a,b)|c\]

When the equation has a solution.

Then solve the equation. . .

I think that is probably:

We set

\[ax_1+by_1=gcd(a,b)\]

\ [Bx_2 + (a \ way b) y_2 = GCD (b, a \ way b) \]

Euclidean and \ (a \ bmod b = a- \ lfloor a / b \ rfloor \) have

\[ax_1+by_1=bx_2+(a-\lfloor a/b\rfloor)y_2\]

\[ax_1+by_1=ay_2+bx_2-\lfloor a/b\rfloor y_2\]

According Identical Theorem (?) Was:

\[x1=y2,y1=x2-\lfloor \frac{a}{b} \rfloor *y2\]

Then we know, \ (gcd (A, b) | c \) .

Then we calculate \ (ax + by = gcd ( a, b) \) after the answer to, as long as he took on the \ (c / gcd (a, b) \) Jiuhaola.

Anyway, I know the code whines

Code


void exgcd(int a, int b, int &x, int &y)
{
    if(b == 0)
    {
        x = 1;y = 0;
        return;
    }
    exgcd(b,a%b,y,x);
    y-=a/b*x;
    //计算ax+by=gcd(a,b)的值
}

Guess you like

Origin www.cnblogs.com/oierwyh/p/11375115.html
gcd