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;}