China Remainder Theorem
To solve the following problems:
For NMutual QualityReturns the smallest public solution x. x meets the requirements of X mod n1 = A1, X mod n2 = a2 ...... X mod nn =
In our country, there is a mathematical ancient book "Sun Tzu's computing Sutra" with the following question: "Today there are things. I don't know the number. There are three, two, five, and three; seven to seven, two left. Ask ry ?」
This is an integer divided by three integers, divided by more than five three, divided by more than seven two, calculate this INTEGER (meet the conditions and the minimum)
The practice is:
N % 3 = 2, N % 5 = 3, N % 7 = 2 and 3, 5, 7
Find the number of 1 in multiples of (5 × 7) modulo 3. The answer is 70.
Find the number of 1 in multiples of (3 × 7), and the answer is 21.
Find the number of 1 in multiples of (3 × 5) modulo 7. The answer is 15.
So
(70 × 2 + 21 × 3 + 15 × 2) % (3 × 5 × 7) = 23,23 is the final answer.
Understanding:
70 × 2 = 140, where 2 refers to 2 of N % 3 = 2, because 70% 3 = 1, multiply by 2 to make it meet the condition of % 3 = 2. Similarly,
21 × 3 make it meet % 5 = 3
15 × 2 to meet % 7 = 2
In addition, 70 can be divided into 5 and 7, 21 can be divided into 3 and 7, 15 can be divided into 3 and 5
Therefore, 70 × 2 + 21 × 3 + 15 × 2 = 233 can meet the conditions of % 3 = 2, % 5 = 3, % 7 = 2
Modulo 233 to 3x5x7 = 105 to obtain the minimum value, because 105 is able to divide the number of 3, 5, 7 at the same time. If it is subtracted, % 3 = 2, % 5 = 3, % 7 = 2. Therefore, the result of Modulo 105 is the minimum number that meets the conditions.
Similarly, the practice of this question can be compared with the above method.
Find the number of 1 in the modulo 33 of (23 × 28). The answer is 1288.
Find the number of 1 in the formula 28 of (23 × 33). The answer is 14421.
Find the number of 1 in the modulo 23 of (28 × 33). The answer is 5544.
(5544 × P + 14421 × E + 1288 × I) % (23 × 28 × 33) = ans + d
// Chinese Remainder Theorem <br/> # include <iostream> <br/> using namespace STD; <br/> const int P = 23, E = 28, I = 33, N = 21252; <br/> int main () <br/>{< br/> freopen ("in.txt", "r", stdin ); <br/> int P, E, I, D, ANS, n = 0; <br/> while (scanf ("% d ", & P, & E, & I, & D) <br/>{< br/> ++ N; <br/> If (P =-1) break; </P> <p> ans = (5544 * P + 14421 * E + 1288 * I-d + n) % (P * E * I ); </P> <p> If (ANS = 0) <br/> ans = N; <br/> else <br/> ans = (ANS + n) % N; </P> <p> printf ("case % d: The next Triple peak occurs in % d days. /n ", N, ANS); <br/>}< br/> return 0; <br/>}