C ++ implementation, expanding Chinese remainder theorem - Solution of a congruence equation (theoretical proof and code implementation)

Expanding Chinese remainder theorem

Foreword

Remember also wrote a blog about the expanding Chinese remainder theorem six months ago. . . But then it is not a deeper understanding and writing was chaotic.

So take advantage of school review of the machine, again remind ourselves to expand Chinese remainder theorem (hereinafter referred to as ExCRT)

Remember also wrote a blog about the expanding Chinese remainder theorem six months ago. . . But then it is not a deeper understanding and writing was chaotic.

So take advantage of school review of the machine, again remind ourselves to expand Chinese remainder theorem (hereinafter referred to as ExCRT)

Some theoretical preparation

Expand Euclid Solving Indefinite Equation

For indeterminate equation \ (A * X + B * Y = GCD (a, b) \) , depending on a, b are constants, we have a generic way to find a group of specialized Solution:

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

Note: This method can only find one kind of special solutions for all solutions, please ask the reader to Baidu. This function runs in the end, x, y is a group of specialized solutions, the return value is \ (GCD (A, B) \) .

So we can promote this equation for the solution similar to \ (a * x + b * y = c \) this equation, the equation is to first find the \ (a * x '+ b * y' = gcd (a, B) \) solution, equation after multiplying both sides simultaneously \ (C / GCD (a, B) \) . It will be converted to:

\(a*x'*c/gcd(a,b)+b*y'*c/gcd(a,b)=c\),\(x=x'*c/gcd(a,b),y=y'*c/gcd(a,b)\).

If c are not to be \ (gcd (a, b) \) divisible, then the indefinite equation has no solution

Proof theory

For congruence equation:

\(\left\{\begin{matrix}x\equiv r_{1}(mod\: m_{1}) & & \\ x\equiv r_{2}(mod\: m_{2}) & & \\ ...... & & \\x\equiv r_{k}(mod\: m_{k}) \end{matrix}\right.\)

We can consider \ (k = 2 \) case, which is the solution of the following equation:

\(\left\{\begin{matrix} x\equiv r_{1}(mod\: m_{1})\\ x\equiv r_{2}(mod\: m_{2}) \end{matrix}\right.\)

Difficult by the above equation is converted to the form

\(\left\{\begin{matrix} x=k1*m1+r1\\ x=k2*m2+r2 \end{matrix}\right.\)

The two formulas can be merged:

\(k1*m1-k2*m2=r2-r1\)

For this equation, we can use to expand Euclid solved.

To solve the equation \ (K1 'M1-K2 *' * GCD = M2 (M1, M2) \) , can then be obtained

\ (K1 = K1 '* (R2-R1) / GCD (M1, M2) \) , into expressions \ (x = k1 * m1 + r1 \) can set Laid solution x is calculated, provided that Special Solution as \ (X0 \) , you can get the general solution

\ (the X-X0 = + t * LCM (M1, M2) \) , so we will get a new congruence equation by combining the 1st, 2nd congruence equation: \ (the X-\ equiv X0 (MOD \: lcm (m1, m2)). \)

This congruence equation combined in the same manner as with the third formula I, last only a single expression \ (x \ equiv x_ {k } (mod \: lcm ( all modulo m_ {i}) ) \) .

At this point the answer we can draw the smallest answer.

Code

LL ExCRT() {
    LL M=m[1],R=r[1];
    //方便 理解代码的话
    //m可以看作上述x0,R可以看作lcm(m1,m2)
    for(LL i=2,d,x,y;i<=n;i++) {
        d=exgcd(M,m[i],x,y);//d为最大公约数
        if((R-r[i])%d) return -1;//无解的情况
        x=x*(R-r[i])/d%m[i];
        R-=x*M;
        M=M/d*m[i];
        R%=M;
    }
    return (R%M+M)%M;//最小的正整数解
}

Written on the back

Now found. . . ExCRT good simple

Guess you like

Origin www.cnblogs.com/MisakaMKT/p/11626929.html