**Introduction to China Residue Theorem**

In Sun Tzu's computing Sutra, there is a problem: "I don't know the number of things today, but the number of three is two (divided by more than three 2 ), three of the five remaining values (divided by more than 5 3), two of the seven remaining values (divided by more than 7 2), and asked ry?" This is called the "Sun Tzu's question ",

The Ming Dynasty mathematician, Cheng Damai, hinted at the solution of this question with four very popular statements in his "algorithm Zong" (1593:

Three people walk 70 times in the same industry,

Plum blossom tree,

Half a month,

You will know about it in addition.

"Half a month" implies 15. The original intention of "divide by 105" is that when the obtained number ratio is large, it will be continuously reduced by 105 until it is smaller than 105. This is equivalent to using 105 to remove and obtain the remainder. Expressed in Mathematics (70*3 + 21*5 + 15*7) % 105 = 23

The general solution to this problem is internationally referred to as the "China Residue Theorem ". The specific solution can be divided into three steps:

1. Find three public multiples:

Find the minimum number 15 after 7 division of 1 from the public multiples of 3 and 5;

From the public multiples of 3 and 7, find the minimum number 21 that is divided by 5 and the remainder 1;

Find the minimum number 70 except 3 and 1 from the public multiples of 5 and 7.

2. Calculate the sum of the three numbers after slight changes:

Multiply by 15 by 2 (2 is the remainder of the final result divided by 7 );

Multiply by 21 by 3 (3 is the remainder of the final result divided by 5 );

Multiply 70 by 2 (2 is the remainder of the final result divided by 3 );

Add the three products (15*2 + 21*3 + 70*2) to 233.

3. Find the minimum number that meets the conditions:

Divide the sum of the three products by 233 by 3, 5, and 7 by the minimum public multiple of 105, and obtain the remainder 23, that is, 233% 5 = 23. The remainder 23 is the minimum number that meets the condition.

**Analysis of China's residue theorem (mutual quality)**

We can break down the "Sun Tzu problem" into several simple small problems.

First, let us assume that N1 is a number that satisfies division by 3 and more than 2, such as 2, 5, 8, and so on. Similarly, we assume that N2 is a number that satisfies the division by more than 5 3, and N3 is a number that satisfies the division by more than 7 2.

With these assumptions, we first analyze from the perspective of N1. If we know that N1 is satisfied by dividing by more than 3 2, can we make the sum of N1 + N2 still meet the condition of dividing by 3 + 2? So that the sum of N1 + N2 + N3 is still divided by 3 + 2?

A basic mathematical theorem is involved here. If a % B = C exists, (a + kb) % B = C (K is a non-zero integer). In general, if the remainder of a division operation is C, the sum (difference) of the addition (or subtraction) of the divisor and K times is then divided with the divisor, and the remainder remains unchanged.

According to this theorem, if N2 is a multiple of 3, N1 + N2 is still satisfied with dividing by 3 + 2. Similarly, if N3 is a multiple of 3, the sum of N1 + N2 + N3 is divided by 3 and 2. This is from the perspective of N1, and from the perspective of N2 and N3, we can export the following three points:

1. to divide the sum of N1 + N2 + N3 by 3 + 2, N2 and N3 must be multiples of 3.

2. to divide the sum of N1 + N2 + N3 by more than 5 3, N1 and N3 must be multiples of 5.

3. to divide the sum of N1 + N2 + N3 by more than 7 2, N1 and N2 must be multiples of 7.

Therefore, to enable the sum of N1 + N2 + N3 as a final solution to the "Sun Tzu problem", we need to meet the following requirements:

1. N1 is divided by more than 3 2 and is a public multiple of 5 and 7.

2. N2 is divided by more than 5 3 and is a public multiple of 3 and 7.

3. N3 is divided by more than 7 2 and is a public multiple of 3 and 5.

Therefore, the essence of Sun Tzu's solution is to find a number N1 divided by 3 and 2 in the public multiples of 5 and 7, find a number N2 divided by more than 5 3 from the public multiples of 3 and 7, and find a number N3 divided by more than 7 2 from the public multiples of 3 and 5, then, add the three numbers to obtain the solution. (There is a small trick in finding N1, N2, N3. Taking N1 as an example, we do not directly find a number divided by 3 and 2 from the public multiples of 5 and 7, instead, first find a number divided by 3 and then multiply by 2 .)

Here is another mathematical formula. If a % B = C, (A * k) % B = A % B +... + A % B = C +... + C = Kc (k> 0). That is to say, if the remainder of a division is C, the remainder of the devisor K times the division is KC. Such as proof in the expansion.

Finally, we should also note that N1 + N2 + N3 is only a solution to the problem, not the smallest solution. How do I obtain the explain solution? We only need to minimize the loss of the Public multiples of 3, 5, and 7 by 105. The truth is the previous theorem "if a % B = C, there is (a-KB) % B = C ". Therefore (N1 + N2 + N3) % 105 is the final solution.

The above is just a simple analysis process for Sun Tzu's question. What if we don't just give three groups? But 10, 20, or more? Next we will conduct an in-depth analysis on the Chinese residue theorem and explore a solution.

There are N groups of data. The divisor of each group is P [I], and the remainder is R [I] (1 <= I <= N). Based on the analysis of the Sun Tzu problem, we know that we should first go to P [2], p [3],..., In the public multiples of P [N], find a number num [1] divided by P [1] remainder R [1], in P [1], p [3],…, In the public multiples of P [N], find the number num [2] divided by P [2] remainder R [2],..., In P [1], p [2],..., In the public multiples of P [n-1], find a number num [N] divided by P [N] remainder R [N], then replace num [1], num [2], num [3],..., Num [N] accumulate sum, and finally use sum to P [1], p [2],..., Returns the remainder of the minimum public multiple of P [N]. The obtained number is the minimum integer that meets the condition.

Note: Sun Tzu's problem analysis is to find the sum of all numbers and obtain the remainder. When the number of groups is small, this is okay, but when the number of groups is too large, there must be more, here we can use the formula (a + B) % C = (a % C + B % C) % C to optimize the program.

**Solution code of the program algorithm:**

The Code is as follows:

Code to run: C/C ++ code

# Include <stdio. h ># include <iostream> using namespace STD; // Extended Euclidean Algorithm int exgcd (int A, int B, Int & X, Int & Y) {int D; if (B = 0) {x = 1; y = 0; return a;} d = exgcd (B, A % B, Y, X ); y-= A/B * X; return D;} // returns the Chinese Remainder Theorem. R [] stores the remainder, prime [] stores the number of mutual quality int chinese_remainder (int r [], int prime [], int Len) {int I, d, X, Y, m, n = 1, sum = 0; // calculate the product N of the divisor, which is also the smallest public multiple of the divisor for (I = 0; I <Len; I ++) N * = prime [I]; // calculate the number of for (I = 0; I <Len; I ++) {M = N/prime [I]; // Calculate the product m d = exgcd (prime [I], M, x, y) that removes all divisor values ); // calculate W [I] * x + M * Y = gcd (W [I], M) A solution of Y // The accumulation integer of y solves the same of Y and constantly remainder n. The formula is used: (A + B) % C = (a % C + B % C) % c Sum = (sum + y * m * R [I]) % N;} return (n + sum % N) % N; // partial solution meeting the equation} int main () {int N, I; int prime [15], R [15]; while (printf ("Enter the number of groups N: \ n "), scanf (" % d ", & N )! = EOF) {printf ("Please input the divisor and remainder of each group in sequence: \ n"); for (I = 0; I <n; I ++) {scanf ("% d", & prime [I], & R [I]);} // printf ("% d \ n", chinese_remainder (B, w, n); printf ("minimum qualified INTEGER: % d \ n", chinese_remainder (R, Prime, n);} return 0 ;}

Core code:

int Chinese_Remainder(int r[],int prime[],int len){ int i,d,x,y,m,n=1,sum=0; for(i=0;i<len;i++) n*=prime[i]; for(i=0;i<len;i++) { m=n/prime[i]; d=exgcd(prime[i],m,x,y); sum=(sum+y*m*r[i])%n; } return (n+sum%n)%n;}