#Q219. 「一本通 6.4 练习 3」Biorhythms

「一本通 6.4 练习 3」Biorhythms

Description

Original source: POJ 1006

Humans are born with three biological cycles: physical, emotional, and intellectual, with cycle lengths of 2323 days, 2828 days, and 3333 days, respectively. Each cycle has one peak day. On the peak day, a person excels in the corresponding aspect. For example, during the peak of the intellectual cycle, one might think more sharply and concentrate more easily. Because the cycle lengths differ, the peaks of the three cycles usually do not occur on the same day. For each individual, we want to know when all three peaks coincide. For each cycle, we are given the number of days from the first day of the current year to the peak day (not necessarily the first occurrence of the peak).

Your task is, given a starting day (counted from the first day of the year), to output the next time all three peaks coincide (the number of days after the given time, not including the given time itself).

For example: if the given time is 1010, and the next occurrence of all three peaks on the same day is 1212, then output 22 (note that it is not 33).

Input Format

There are multiple test cases.

For each test case, input four integers p,e,ip, e, i, and dd. Here, p,e,ip, e, i represent the days from the first day of the year to the peak days of the physical, emotional, and intellectual cycles, respectively. dd is the given starting day, which may be smaller than p,ep, e, or ii.

The input ends when p=e=i=d=1p = e = i = d = -1.

Output Format

From the given starting day, output the next occurrence when all three peaks coincide (the number of days after the given time). Use the following format:

Case 1: the next triple peak occurs in 1234 days.

Note: Even if the result is 11 day, use the plural form days.

Sample 1

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

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.

Constraints & Hint

All given times are non-negative and less than 365365, and the sought time is less than 2125221252.