Poj 1006 biorhythms (China residue theorem)

Source: Internet
Author: User
Biorhythms
Time limit:1000 ms   Memory limit:10000 K
Total submissions:94863   Accepted:29139

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
Three cycles generally occur at different times. We wowould like
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
Number of days from the beginning of the current year at which one
Its peaks (not necessarily the first) occurs. You will also be given
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
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,
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 cases. The input for each case consists of one
Line of four integers p, E, I, and D. The values P, E, and I are
Number of days from the beginning of the current year at which
Physical, emotional, and intellectual cycles peak, respectively.
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
Triple peak will occur within 21252 days of the given date. The end
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 23 102 320203 301 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.

Chinese Remainder Theorem: same remainder 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 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 is, for the numbers N1, N2, and N3, the modulo values of a certain number of m pairs N1, N2, and N3 are respectively A, B, and C;

A = m % N1;

B = m % N2;

C = m % N3;

Evaluate M.

(1) Evaluate the number of M3 with the MOD N3 as 1 in the multiples of the minimum public multiples of N1 and N2.

(2) Evaluate the number of M2 where N2 is 1 in a multiple of the least common multiples of N1 and N3.

(2) Calculate the number M1 of the modulo N1 as 1 in a multiple of the least common multiples of N1 and N2.

M = (m1 * A + m2 * B + M3 * C) % (N1 * N2 * N3)

 /*  * Chinese Remainder Theorem: Same Remainder Theorem  */  # Include <Stdio. h> Int  Main (){  Int P, E, I, D, K = 1  , Days;  Int  Num1, num2, num3;  For ( Int J =1 ; J ++ )  If ( 28 * 33 * J % 23 = 1 ) {Num1 = 33 * 28 * J; Break  ;}  For ( Int J = 1 ; J ++)  If ( 23 * 33 * J % 28 = 1 ) {Num2 = 23 * 33 * J; Break  ;}  For ( Int J = 1 ; J ++ )  If ( 23 * 28 * J % 33 = 1 ) {Num3 = 23 * 28 * J; Break  ;}  While (Scanf ( "  % D  " , & P, & E, & I, & D )! = EOF ){  If (P =-1 & Amp; E =- 1 & I =- 1 & D =- 1 ) Break  ; Days = (P * num1 + E * num2 + I * num3-d) % ( 23 * 33 * 28  );  If (Days <= 0 ) Days = 21252 -D; printf (  "  Case % d: The next Triple peak occurs in % d days. \ n  " , K ++ , Days );}  Return   0  ;} 

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.