Explanation of Chinese Remainder Theorem, explanation of Remainder Theorem
China Remainder Theorem
There is a question in Sun Tzu's Computing Theory: "Today there are things that don't know the number of things. There are two three, three, five, and three, and seven, and there are two. Are there things in ry ?"
The current mathematical problem is x % 3 = 2, x % 5 = 3, x % 7 = 2, and returns the value of x;
Many beginners of C language can't help but think of using brute force to calculate this problem. What else should I do with such a theorem?
What if the data is big? Computing on the computer is quite difficult. However, this problem was solved by Sun Tzu early.
Find the least common factor lcm in the three, five, and seven, and k * lcm is equal to 1 (find a k that meets the condition );
The remainder of the equation using k * lcm * that does not have a number in lcm [(a bit round) is lcm ), the other number is the remainder of 7 in the equation x % 7 = 2. That is, finding the k * lcm (3, 5) * 2].
Method (concept of residue theorem ):
Lcm (3, 5) = 15; // The lcm is the smallest public multiple.
Lcm (3,7) = 21;
Lcm (5, 7) = 35;
A * 15% 7 = 1;
B * 21% 5 = 1;
C * 35% 3 = 1;
The values of a, B, and c are, 2;
We can get 15% 7 = 1, 21% 5 = 1, 70% 3 = 1;
Ans = (15*2 + 21*3 + 70*2) % lcm (3, 5, 7 );
Ans = 23;
Add an example:
What is the minimum value of a number that is 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;
Therefore, 274% 60 = 34 is the number of requests.
Stick a residue theorem question POJ 1006 http://poj.org/problem? Id = 1006
The question has Chinese meanings.
The solution to this question is:
Known (ans + d) % 23 = a; (ans + d) % 28 = B; (ans + d) % 33 = c
Divide 33 × 28 × X by 23 and divide 1 by 33 × 28 × 8 = 5544;
So that 23 × 33 × Y is divided by 28 to 1, with 23 × 33 × 19 = 14421;
So that 23 × 28 × Z is divided by 33 to 1, with 23 × 28 × 2 = 1288.
So X = 8, Y = 19, Z = 2;
So there are (5544 × a + 14421 × B + 1288 × c) % lcm (, 28, 33) = ans + d
Another 23, 28, 33, I .e., lcm (21252, 28, 33) =;
So there are ans = (5544 × a + 14421 × B + 1288 × c-d) % 21252
AC code
#include<iostream>#include<stdio.h>using namespace std;int main(){int a, b, c, d,cnt=1;int ans;while (scanf("%d%d%d%d", &a, &b, &c, &d)!=EOF&&a!=-1){ans = (a * 5544 + b * 14421 + c * 1288) % 21252;ans -= d;if (ans<=0) ans += 21252;if(ans>21252) ans=21252;printf("Case %d: the next triple peak occurs in %d days.\n", cnt++, ans);}return 0;}
This question can be exploited too.
Violent AC code
#include<iostream>#include<stdio.h>using namespace std;int main(){ int a, b, c, d,cnt=1,i; while (scanf("%d%d%d%d", &a, &b, &c, &d)!=EOF&&a!=-1) { for(i=1; ; i++) if((i-a)%23==0&&(i-b)%28==0&&(i-c)%33==0&&i>d) { break; } printf("Case %d: the next triple peak occurs in %d days.\n", cnt++, i-d); } return 0;}
China's law of Surplus
That is, in fact, the question of "Han Xin dianbing", there are also such arithmetic questions in Sun Tzu's computing Sutra: "Today there are things that do not know the number of things, and there are only two things left, there are three to five, and two to seven?" In today's words, divide a number by 3 to 2, divide by 5 to 3, and divide by 7 to 2. this problem is also called "Han Xin dianbing ". it forms a kind of problem, that is, the homophone formula in elementary number theory. the conditional solution to such problems is called the "China Residue Theorem", which was first proposed by the Chinese. in fact, this type of problem is the application of the least common multiple and the maximum common number.
For more public exam information, see Anhui talent Information Network www.ahrcw.com
Returns the Chinese Remainder Theorem.
For the convenience of using X ≡ a (mod m) to represent X, division of the remainder by m is a, and aX ≡ B (mod m) to represent that aX uses m to divide the remainder by B, which is called the same remainder, the two questions are as follows:
1. Find that X meets the same-remainder Group
X sector 1 (mod3), X region 2 (mod4), X region 4 (mod5 ),
First, calculate X1, X2, and X3. they satisfy the same remainder.
20X1 limit 1 (mod3), 15X2 limit 2 (mod4), 12X3 limit 4 (mod5 ),
(20 = 4 × 5, 15 = 3 × 5, 12 = 3 × 4)
X1 limit 2 (mod3), X2 limit 2 (mod4), X3 limit 2 (mod5), (how to solve the problem below)
It is a coincidence to find that the three cooperators above are 2 or 3,
Therefore, X 20X2 + 15X2 + 12X2 94 (mod60) and X = 34 are obtained.
2. Find that X meets the same-remainder Group
X Round 5 (mod9), X Round 1 (mod7), X Round 2 (mod5 ),
First, calculate X1, X2, and X3. they satisfy the same remainder.
35X1 limit 5 (mod9), 45X2 limit 1 (mod7), 63X3 limit 2 (mod5 ),
X1 limit 4 (mod9), X2 limit 5 (mod7), X3 limit 4 (mod5 ),
Therefore, X 35 × 4 + 45 × 5 + 63 × 4 × 617 (mod315), X = 302.
How can I find the same formula as 20 X1 running 1 (mod3), 15X2 running 2 (mod4), 12X3 running 4 (mod5), etc? for middle school students, I just need to try it out, as long as 3, 4, 5 ing (9, 7, 5 ing), must be resolved in the same formula, such as 12X 1274 (mod5), convert X = ,..., 5. Try substitution. When X = 1, 12X uses 5 to divide the remainder 2, X = 2, and 12X to divide the remainder 4 with 5. Therefore, X = 2 is the solution, and the test method does not calculate much, m = 5, try up to 5 times, m = 9, try up to 9 times, such as 35X limit 5 (mod9), try up to 9 times, convert X =, 3 ,..., 9. If you don't want to use the trial method, there are many methods, but it is better to use the simple method, such as calculating 12X ≡ 4 (mod5). (1) One is an indefinite equation, 12X percentile 4 (mod5) is equivalent to the integer solution of the binary indefinite equation 12X-5Y = 4. It can be obtained by Euclidean division of the moving phase.
(2) first obtain the solution of 12X 1_1 (mod5) and use the Euler's theorem (this is the important theorem of number theory a ^ P-1) 1_1 (modp )) obtain the values of X 12 ^ (5-2) (mod5), X 12 12 ^ 3 then 2 ^ 3 = 8 then 3, 12X then 4 (mod5) the solution is X ≡ 4 × 3 ≡ 2 (mod5). In this method, the homogeneous formula aX ≡ B (modm) requires a and m.
See one of my articles:
Hi.baidu.com/...4.html
Hi.baidu.com/...7.html