1 /*2 Congruence Equation Group:3 set positive integers m1.m2.mk 22, then equations4 X≡A1 (mod m1)5 x≡a2 (mod m2)6 x≡a3 (mod m3)7 .8 .9 X≡ak (mod mk)Ten There are integer solutions, One solution for X≡ (A1 * M1 * 1/m1 + A2 * M2 * 1/m2 + a3 * M3 * 1/m3 + ... +ak * Mk * 1/mk) mod M A WHERE M = M1 * M2 * M3 * ... * Mk, MI is M/mi, 1/mi is the inverse of Mi - */ - the voidEXGCD (intAintBint&x,int&y) - { - if(b = =0) { -x =1; +y =0; - returnA; + } AEXGCD (b, a%b, x, y); at intt =x; -x =y; -y = t-a/b *y; - returnR; - } - inLL CRT (intM[],intA[],intN//M is the number of mods, A is the remainder, n is the number of equation groups - { toLL M =1, ans =0; + for(inti =0; I < n; i++) -M *=M[i]; the for(inti =0; I < n; i++) { * intx, y; $LL Mi = M/M[i];Panax NotoginsengEXGCD (Mi, M[i], x, y);//The inverse of x, Mi, is obtained -Ans = (ans + Mi * a[i] * x% m + m)%M; the } + returnans; A}
Chinese remainder theorem template