POJ 1006: Biorhythms Chinese Remainder Theorem

Source: Internet
Author: User

POJ 1006: Biorhythms Chinese Remainder Theorem

 

Biorhythms
Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:121194   Accepted:38157

 

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.

In a person's life, there are three biological rhythms: body, emotion, and intelligence. Each emotion has a corresponding cycle. At this point in the cycle, this aspect of the person has become particularly embarrassing.

Now, the three cycles are 23, 28, and 33. This is fixed.

Then, I will give you the peak time of the last three cycles, which is p e I. I will ask you how long it will take for N days from the start of the d period to reach the peak of the three cycles.

The column formula is N + d = p + 23 * N1 = e + 28 * N2 = I + 33 * N3.

N1, N2, and N3 are integers. N is required.

The next conversion is to find the smallest number of S, which is divided by 23 p, divided by 28 e, divided by 33 I. Of course, the N that is finally required only needs S-d.

Another rising posture... China residue theorem.

First, find the minimum number of conditions that meet the division by 23 + 1, divide by 28, and divide by 33. The result is 5544.

Then, the minimum number of conditions that meet the Division divided by 28, divide by 23, and divide by 33 is 14421.

Then, the minimum number of conditions that meet the Division divided by 33 and more than 1, divide by 23, and divide by 28 is 1288.

Then 5544 * p + 14421 * e + 1288 * I must be the next peak of three cycles, but not necessarily the minimum value.

 

Here, I will explain the reasons for doing so:

1. If A number A is divided by 23 and B is divided by 23, then (A + B) is divided by 23 and it is still the remainder of 1. That is to say, if a number is added with the total number of x, the remainder of x is not changed.

Therefore, for 23, the remainder of A + B + C is still 1, because B and C divide 23.

For 28, the remainder of A + B + C is still 1, because A and C are divided by 28.

For 33, the remainder of A + B + C is still 1, because A and B divide 33.

2. If A is divided by 23 and A * c is divided by 23, the remainder is c * 1, that is, the remainder is c.

Therefore, the conclusion is that 5544 * p is divided by 23 and the remainder p is divided by 28 and 33.

14421 * e divided by more than 28 e, divided by 23, divided by 33

1288 * I divided by 33 I, divided by 23, divided by 28

 

Therefore, the sum of the three conditions is met, but it is not necessarily the minimum value. How to calculate the minimum value % lcm (, 29) is enough.

Code:

 

#include 
 
  #include #include 
  
   #include 
   
    #include 
    
     #include 
     
      #pragma warning(disable:4996)using namespace std;int main(){int a,b,c,d,j=1;while(cin>>a>>b>>c>>d){if(a+b+c+d==-4)break;cout<
      
       

 

 

 

 

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.