菜单

POJ刷题计划 POJ-1006 中国剩余定理

八月 17, 2018 - 算法
POJ刷题计划 POJ-1006 中国剩余定理

Biorhythms

题目描述

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 would 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 should 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 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 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

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.

题目大意

题干有点长,大概的意思是说,人的体内有三个周期:身体周期,情感周期,智力周期,分别对应的长度是23天,28天和33天,也就是说存在一个大的周期使得三个循环出现在同一天,把这一天称为三个高峰点,现在给出某一年中三个周期出现的天数和从这一年开始算起的第n天,求这天之后再次出现三个高峰的天数。
举个栗子:
283 102 23 320表示从第283天、102天、23天分别出现了身体周期、情感周期、智力周期的高峰点,要求的是第320天后首次出现三个高峰点的天数。

题目分析

好不容易看懂了题意,这题的思路却不是很好理清。
其实把题目抽象来看,设三个高峰点出现的天数分别是x1,x2,x3,那么下一次身体周期高峰出现的时间是第x1+23天,下一次情感周期高峰出现在x2+28天,而智力周期高峰在第x3+33天,类推下去的话,第n次单个高峰出现的时间可以表示成23n+x1, 28n+x1, 33n+x3。这也就表面,如果有一天X是三个高峰同时出现的时候,那么X一定同时满足上面三个式子,也就是说:
X % 23 = x1
X % 28 = x2
X % 33 = x3
这其实就是中国剩余定理的表示方法,我们要做的其实就是解开这个同余方程得到X的值,这里利用拓展欧几里得算法和中国剩余定理解决问题。借用了一下雪岩聚聚写的模板解决问题。

代码实现

总结

第一次自主做出来这种数学性比较强的问题,中国剩余定理在ACM里的应用其实是非常广泛的,再加上快速乘和拓展欧几里得,这几个东西一定得掌握一个。

标签:, ,

发表评论

电子邮件地址不会被公开。 必填项已用*标注