Reprinted ---- poj 1006 China Residue Theorem

Source: Internet
Author: User
This article is reprinted, Source Address: http://blog.csdn.net/dongfengkuayue/article/details/6461298 poj 1006 biorhythms
Time limit:1000 ms   Memory limit:10000 K
Total submissions:78980   Accepted:23740

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. these three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. there is one peak in each period of a cycle. at the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental ). for example, if it is the mental curve, thought processes will be sharper and concentration will be easier. since the three cycles have different periods, the peaks of the three cycles generally occur at different times. we wowould like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. for each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. you will also be given a date expressed as the number of days from the beginning of the current year. you task is to determine the number of days from the given date to the next Triple peak. the given date is not counted. for example, if the given date is 10 and the next Triple peak occurs on Day 12, the answer is 2, not 3. if a triple peak occurs on the given date, you shoshould give the number of days to the next occurrence of a triple peak.

Input

You will be given a number of instances. the input for each case consists of one line of four integers p, E, I, and D. the values P, E, and I are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. the value D is the given date and may be smaller than any of P, E, or I. all values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. the end of input is indicated by a line in which P = E = I = d =-1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next Triple peak, in the form:
Case 1: The next Triple peak occurs in 1234 days.
Use the plural form ''days ''even if the answer is 1.

Sample Input

0 0 0 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 301 203 40-1 -1 -1 -1

Sample output

Case 1: the next triple peak occurs in 21252 days.Case 2: the next triple peak occurs in 21152 days.Case 3: the next triple peak occurs in 19575 days.Case 4: the next triple peak occurs in 16994 days.Case 5: the next triple peak occurs in 8910 days.Case 6: the next triple peak occurs in 10789 days.
This article is transferred from the head for better blog, with all rights reserved. The code is compiled by myself.
Problem description
People have three physiological cycles: and 33 days since birth. One day in a cycle is the peak value. On this day,
People have the best performance in terms of physical strength, emotion, or intelligence. Generally, the peak values of these three cycles are not the same day.
Now, three dates are given, corresponding to the time when physical, emotional, and intellectual peaks occur. Then a start date is given,
It is required to calculate the minimum number of days after which the three peaks will appear at the same time from this day.
Problem Analysis
First, we need to know that the period between any two peaks must be an integer multiple. Assume that the nth day of the year reaches the peak value, and the next time the peak value is reached
Between them is n + tk (T is the cycle, K is any positive integer ). Therefore, the day (s) when three peaks appear at the same time should meet
      S = N1 + T1*k1 = N2 + T2*k2 = N3 + T3*k3
N1, N2, and N3 are the dates of physical strength, emotion, and peak mental retardation, T1, T2, and T3 respectively.
We need to obtain the three non-negative integers K1, K2, and K3 so that the equation above is true.
It seems difficult to find K1, K2, and K3 directly, but our goal is to find S. We can consider reverse push from the result.
According to the equation above, s meets three requirements: divide by the remainder of T1 into N1, divide by the remainder of T2 into N2, and divide by the remainder of T3 into N3.
In this way, we convert the problem into a minimum number. This number is divided by T1 remainder N1, T2 remainder N2, and T3 remainder N3.
This is the famous Chinese surplus theorem. Our ancestor has come up with a subtle solution to this problem thousands of years ago.
Based on this algorithm, the time complexity can reach O (1 ). The following describes the Chinese Remainder Theorem.
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 to five (divided by more than five ),
Seven days left two (divided by more than seven 2), ask ry ?" This problem is called the "Sun Tzu's Problem". The general solution of this problem is internationally referred to as the "China Surplus Theorem ".
The solution consists of three steps:
  1. Find three numbers: Find the minimum number 15 after 7 division of 1 from the public multiples of 3 and 5, and find the minimum number 21 after 5 Division of 1 from the public multiples of 3 and 7, finally, find the minimum number of 70 except 3 and 1 from the public multiples of 5 and 7.

2. multiply by 15 by 2 (2 is the remainder of the final result divided by 7) and 21 by 3 (3 is the remainder of the final result divided by 5). Likewise, multiply 70 by 2 (2 is the remainder of the final result divided by 3), and then add the three products (15*2 + 21*3 + 70*2) to get and 233.

 

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

That's simple. While sighing the magic, we can't help wondering how the ancients thought of this method. Is there any basic mathematical basis?

China residue theorem Analysis

We split the "Sun Tzu's question" into several simple small problems, starting from scratch, trying to speculate on how the ancients pushed this solution.

First, let us assume that N1 is a number that satisfies dividing by 3 more than 2, such as 2, 5, 8, and so on, that is, an arbitrary number that satisfies 3 * k + 2 (k> = 0. 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 the preceding assumption, we first start from the perspective of N1. If we know that N1 is satisfied by dividing by 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?

This involves a basic mathematical theorem. If a % B = C exists, (a + kb) % B = C (K is a non-zero integer). In other words, 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. This is a good proof.

Based on this theorem, if N2 is a multiple of 3, N1 + N2 can still be divided 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. From the perspective of N2 and N3, we can export the following three points:

  1. In order to divide the sum of N1 + N2 + N3 by 3 + 2, N2 and N3 must be multiples of 3.
  2. In order 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, and find one in the public multiples of 3 and 7.

Divide by the number N2 of 5 plus 3, find a number N3 divided by 7 plus 2 from the public multiples of 3 and 5, and then add the three numbers to get the solution.

I used a small trick to evaluate N1, N2, and N3. Taking N1 as an example, we did 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, then the remainder of the division K times is KC. Proof in expansion.

Finally, we need to be clear 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.

Summary

After analysis, it is found that there is no advanced technique for the Sun Tzu solution of China's residual theorem, that is, the flexible use of the following two basic mathematical theorems:

  1. If a % B = C, there is (a + kb) % B = C (K is a non-zero integer ).
  2. If a % B = C, (A * k) % B = Kc (K is an integer greater than zero ).
Poj1006 is not a programming problem, but it uses the remainder theorem. First, let's talk about the mathematical computation. You can search for "Residue Theorem" or "Han Xin dianbing" on the Internet ".
Because the numbers 23, 28, and 33 are mutually compatible,
Then, the remainder theorem includes: R1 is the smallest positive integer (A is a positive integer) with 28*33 * A % 23 = 1, that is, a = 6, r1 = 5544 R2 is the smallest positive integer 23*33 * B % 28 = 1 (B is a positive integer), that is, B = 19, r2 = 14421 R3 is the smallest positive integer of 23*28 * C % 33 = 1 (C is a positive integer), that is, c = 2, r3 = 1288 or less is the c ++ source code of the AC:

 

Source code

Problem:1006   User: 
Memory:136 K   Time:0 ms
Language:C   Result:Accepted
  • Source code
    #include <stdio.h>int main(){ int p,e,i,n,sum,t; t=0; while(scanf("%d%d%d%d",&p,&e,&i,&n),p+e+i+n!=-4) {  sum=(5544*p+14421*e+1288*i)%21252;  if(sum-n<=0)   sum+=21252;  printf("Case %d: the next triple peak occurs in %d days./n",++t,sum-n); }return 0;}

Reprinted ---- poj 1006 China Residue Theorem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.