Related Knowledge points:
1. A ≡ B (modc), A and B are equivalent to a % C = B, that is, a modc = B mod C.
2. If a and B are mutually qualitative (a, B) = 1, obtain the inverse ax 1_1 (modb) of a about model B)
3. Theorem on the remainder:
Theorem 1: If the divisor is an integer multiple of (or minus) the divisor, And the divisor remains unchanged, the remainder remains unchanged.
Theorem 2: If the divisor is expanded (or reduced) several times and the divisor is not changed, the remainder is also expanded (or reduced) by the same number.
Theorem 3: If integer a is divided by natural number B (B = 0) and remainder R is not less than B, then the remainder of 'r divided by B 'is equal to the remainder obtained by dividing 'A' by' B. (The remainder and the divisor are the same as the remainder)
4. China residue theorem:
Set M1, M2 ,..., MK is a positive integer between two integers. For any positive integers A1, A2, A3 ,.., AK homogeneous equations: X 127a1 (mod M1) x 127a2 (mod m2 )... X ≡ AK (mod Mk) must be resolved, and the solution can be written as X ≡ m1p1a1 + m2p2a2 + .... mkpkak (mod m) where M = m1m 2m3 .... mkmi = m/MI, (1 <= I <= K)
Proof: For MI = m/MI, then MI and MI are mutually qualitative, then (mi, mi) = 1, that is, mipi commit 1 (mod mi), -- modulo Inverse
Equivalent to mipi % MI = 1. To make mipi % MI = ai, multiply the devisor by A1 (
Obtained by Theorem 2) The solution to the easily-known equations of the same remainder is X 127m1p1a1 + m2p2a2 +... mkpkak (mod m)
Algorithm template:
int m[maxn],a[maxn];void exgcd(int a,int b,int &d,int &x,int &y){ if(!b) d=a,x=1,y=0; else exgcd(b,a%b,d,y,x),y-=x*(a/b);}intChina(int r){ int M,d,x0,y0,ans=0; for(int i=1;i<=r;i++){ M*=m[i]; } for(int i=1;i<=r;i++){ int Mi=M/m[i]; exgcd(Mi,m[i],d,x0,y0); ans=(ans+Mi*x0*a[i])%M; } if(ans<0) ans+=M; return ans;}
----------------------- Split line ---------------------------The following is a reprinted article that helps you understand the theory of China's surplus theorem ...)
There was a question about "things do not know the number" in Sun Tzu's computing Sutra before and after the.m.: "Today, things do not know the number of things. There are two things after three, three in five, three in five, and two in seven, ry?" The answer is "23 ". -------- This is the legendary "China Residue Theorem ".
In fact, the question means that X % 3 = 2, X % 5 = 3, X % 7 = 2. What is the minimum value of X?
Solution:
1. First, find the three "key numbers" of 3, 5, 7, that is, [5, 7] = 35; [3, 7] = 21; [3, 5] = 15
2. let 35a % 3 = 1, A = 2; let 21B % 5 = 1, B = 1; let 15C % 7 = 1, C = 1 (Here we want to set the remainder to 1 to require the remainder 2. If the remainder is required to be multiplied by 2, and the remainder is required to be 3, multiply by 3, ......)
3. Then, 35*2*2 = 140 21*1*3 = 63 15*1*2 = 30
4. Then 140 + 63 + 30 = 233, because 233> 3*5*7, 233-105*2 = 23
Some examples on Baidu (we will find that the divisor is bright and interactive, obviously ......) :
Example 1: What is the minimum value of A number divided by 3 to 1, 4 to 2, and 5 to 4?
The numbers 3, 4, and 5 are mutually qualitative. Then [4, 5] = 20; [3, 5] = 15; [3, 4] = 12; [3, 4, 5] = 60. In order to divide 20 by 3 to 1, 20 × 2 = 40; so that 15 is divided by 4 to 1, 15 × 3 = 45; so that 12 is divided by 5 to 1, use 12 × 3 = 36. Then, 40 × 1 + 45 × 2 + 36 × 4 = 274, because, 274> 60, so, 274-60 × 4 = 34, is the number of requests.
Example 2: What is the minimum value of A number divided by 3, 2, 7, 4, and 8?
The numbers 3, 7, and 8 are mutually qualitative. Then [168] = 56; [] = 24; [] = 21; [, 8] =. In order to divide 56 into 3 and 1, 56 × 2 = 112; make 24 to 7 and 24 × 5 = 120. So that 21 is divided by 8 and 1, 21 × 5 = 105; then, 112 × 2 + 120 × 4 + 105 × 5 = 1229, because, 1229> 168, so, 1229-168x7 = 53, which is the number of requests.
Example 3: divide a number by 5 plus 4, divide by 8 plus 3, and divide by 11 plus 2 to obtain the minimum natural number that meets the conditions.
The numbers 5, 8, and 11 are mutually qualitative. Then [8, 11] = 88; [5, 11] = 55; [5, 8] = 40; [5, 8, 11] = 440. In order to make 88 be divided by 5 to allow 1, 88 × 2 = 176; so that 55 is divided by 8 to allow 1, 55 × 7 = 385; so that 40 is divided by 11 to allow 1, 40x8 = 320. Then, 176 × 4 + 385 × 3 + 320 × 2 = 2499. Because, 2499> 440, 2499-440 × 5 = 299 is the desired number.