Study Notes--Extended Euclid

foreword

I had some understanding of this algorithm before noip, but noipD1T1 did not expect to expand Europe at all, which means that what I learned before was not solid at all, so now I come to fill the hole

Related concepts:

You can take a look at this dalao's blog first, and at the same time, the proofs of many theorems below are also given in that blog:

https://blog.csdn.net/yoer77/article/details/69568676

Term Deduction & Euclidean Algorithm

Let's first introduce a more subtractive method that appeared in "Nine Chapters of Arithmetic" :

  • For any positive integer \(a>=b\) , there are \(gcd(a,b)=gcd(b,ab)=gcd(a,ab)\)

  • Prove:

    For any common divisor d of a and b, because \(d\mid b,d|a\) , so \(d\mid (ab)\) , so the set of common divisors of a,b is the same as \(b,ab\ ) has the same set of common divisors, and the same is true for \(a, ab\) .

Then there is the famous Euclidean algorithm :

For any positive integer $a,b(b \ne 0) \(yes,\) gcd(a,b)=gcd(b,a\mod b)$

When performing high-precision operations, the term deduction method can be considered instead.

Pei Shu's Theorem ( \(Bézout's\) \(lemma\) )

  • For any integer \(a,b\) , there is a pair of integers that satisfy \(ax+by=gcd(a,b)\)

  • Prove:

    I think the proof given in the Advanced Guide to Algorithm Competition is more helpful:

    In the last step of the Euclidean algorithm, that is, \(b=0\) , there is obviously a pair of integers \(x=1, y=0\) , satisfying \(a*1+b*0=gcd(a, 0)\)

    If \(b > 0\) , then \(gcd(a,b)=gcd(b,a \mod b)\) . Fake

    Suppose there is a pair of integers \(x,y\) , satisfying \(b*x+(a \mod b)*y=gcd(b,a \mod b)\) ,

    Because \(bx+(a \mod b)y\)

    \(=bx+(a-b\lfloor a/b \rfloor)y\)

    \(=ay-b(x-\lfloor a/b \rfloor y)\)

    So make \(x'=x,y'=x-\lfloor a/b \rfloor y\) to get \(ax'+by'=gcd(a,b)\)

    Applying mathematical induction (if you don't know what this is, you can think of it as a recursive or recursive process), the theorem holds.

The above proof process also gives the calculation process of \(x,y\) , which is written in code as

ll ex_gcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1,y=0;
        return a;
    } 
    ll d=ex_gcd(b,a%b,x,y);
    ll z=x;x=y,y=z-(a/b)*y;
    return d;
}

ex_gcd returns \(gcd(a,b)\) and finds a set of solutions \(x,y\)

For \(ax+by=c\) , it has a solution if and only if \(gcd(a,b)\mid c\)

Then, the general solution of this equation can be expressed as:

\(x=(c/d)*x0+k*(b/d)\)\(y=(c/d)*y0-k*(a/d)\)

Where \(k\) takes any integer, \(d\) is \(gcd(a,b)\) .

Why is this? I have searched various blogs without a clear explanation. Driven by my perseverance, I finally found a concise and easy-to-understand proof in the Luogu online class:

  • Prove:

    Let's first consider the case of \(ax+by=gcd(a,b)\) ,

    Obviously \(a*b+b*(-a)=0\) is divided by \(gcd(a,b)\ ) at the same time , it is easy to know that this is feasible

    So \(a*(b/gcd(a,b))+b*(-a/gcd(a,b))=0\)

    So if it is known that \(x0,y0\) is a set of special solutions, that is, \(a*x0+b*y0=gcd(a,b)\) , let \(x=x0+k*b/gcd( a,b)\) , \(y=y0-k*(a/gcd(a,b))\) ,

    Bring in \(ax+by\) to get \(a(x0+k*b/gcd(a,b))+b*(y0-k*(a/gcd(a,b)))\)

    \(=ax0+by0+k*(a*b/gcd(a,b)-b*a/gcd(a,b))\)

    \(=ax0+by0=gcd(a,b)\)

    Then \(ax+by=c\) is also solved very well, so I won't go into details here

related

  • Solving Linear Congruence Equations

    For the equation \(a*x \equiv b \pmod m\) , it can be transformed into \(a*xb \equiv 0 \pmod m\) , you might as well set \((a*x)-b\) as \(m \ ) times \(-y\) , that is \(a*xb=-y*m\)

    get \(a*x+m*y=b\)

    Then see above

  • find the inverse element

    What is an inverse element? See this blog of mine: https://www.luogu.org/blog/Rye-Catcher/solution-p3811

    \(a*x \equiv 1 \pmod b\) is also converted to \(ax+by=1\ )

    Then see above

  • example:

Congruence equation: https://www.luogu.org/problemnew/show/P1082

Frog Dating: https://www.luogu.org/problemnew/show/P1516

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325354453&siteId=291194637