Number Theory II = goalkeeper. =
Chinese remainder theorem:
1. congruence equations:
Group congruence equation refers to the form x≡ai (mod mi) (i = 1,2, ..., k) constituted congruence equation
The main purpose of the Chinese remainder theorem is the solution of a congruence equation, where m1, m2, ..., mk coprime
2. Chinese Remainder Theorem:
So that M = m1 * m2 * ... * mk ( i.e., all the LCM m)
Ti is the smallest positive integer congruence equation M / mi * ti≡1 (mod mi ) solution of
The existence of the x = Σai * M / mi * ti
The general solution x + i * M
Solutions of the smallest non-negative integer (x% M + M)% M
(I admit that this is a copy of orz
Original looks more convenient: https://blog.csdn.net/niiick/article/details/80229217 )
M / mi * ti≡1 (mod mi) may be converted to M / mi * ti + mi * y = 1, and then by seeking ti exgcd
3. Proof:
For the k-th equation
① When i ≠ k When there mk | M / mi, i.e., ai * M / mi * ti≡0 (mod mk)
② This i = k Sometimes, Yes M / mk * tk≡1 (mod mk), immediately ak * M / mk * tk≡ak (mod mk)
故∑ai*M/mi*ti≡ak(mod mk)
4. Code:
(Where LL is a long long, qcm rapid multiplication)
1 LL crt(){ 2 LL bwl=0; 3 for(int i=1;i<=k;++i){ 4 LL x,y; 5 exgcd(M/m[i],m[i],x,y); 6 if(x<0) x=x%m[i]+m[i]; 7 bwl=(bwl+qcm(qcm(a[i],M/m[i]),x))%M; 8 } 9 return (bwl+M)%M; 10 }
The count by Sun Tzu:
"grandchildren count by": Today there was an unknown number, the number of remaining three three two; the number of remaining three fifty-five; seventy-seven the number of the remaining two. He was asked geometry?
"Suanfa case": peer seventy three thin, plum tree, twenty five branches and seven sub-reunion positive half months, with the exception hundred and five will know.
70 = 3 * 5 * 3 = 2,70% = 1,21% 3 * 7 * 5 = 1, 15, 21 (two weeks), 15 = 3 * 5 * 7 = 1%
With 70 * 21 * 2 + 15 * 3 + 2 = 3 * 5 * 233 In addition 7 = 105, 23 is the remainder obtained answers
70 = 3 * 3 * 5 * 7 * 2,21 = 1,15 * 5 * 3 = 1 in the last three formulas multiplier 2,1,1 di is the above-mentioned
Digital kinda lucky 233
Extended Chinese Remainder Theorem:
1. congruence equations:
The main purpose of expansion of the Chinese remainder theorem is the solution of a congruence equation, where m1, m2, ..., mn necessarily coprime
2. Expand the Chinese Remainder Theorem:
A solution of equations with the equations I k-1 order is composed of a front x
And M is the k-1 before modulus lcm
Through the solution of equations of the k-1 before the equation is x + i * M
Will now be added to the k-th equation
Only needs a positive integer t, such that
x+t*M≡ak(mod mk)
It may be converted to M * t + mk * y = ak-x
T is then calculated by exgcd
If this equation has no solution, then the whole congruence equation has no solution
Otherwise, a solution of x + t * M equations for the first k equations
(This is my copy of the original and top as orz)
3. Code:
(Where LL is long long, qcm by fast, three multipliers and two parameters were modulus)
1 LL excrt(){ 2 LL M=m[1],ans=a[1]; 3 for(int i=2;i<=k;++i){ 4 LL x,y; 5 LL d=gcd(M,m[i]); 6 LL c=(a[i]-ans%m[i]+m[i])%m[i]; 7 if(c%d) return -1; 8 exgcd(M,m[i],x,y); 9 x=qcm(x,c/d,m[i]/d); 10 ans+=qcm(x,M,M*m[i]); 11 M*=m[i]/d; 12 years = (M + years% M)% M; 13 } 14 return year; 15 }
4. Details:
1. Some questions have strict digital card, you must use fast ride
Note that the second multiplier 2. rapid multiplication must be positive, to use the general solution processing
3. Each rapid multiplication of modulus not necessarily the same, we need to carefully consider
example:
Luo Gu 3868 Guess
Luo Gu 4777 extended the Chinese remainder theorem