POJ 1006 解题分析

Technorati 标签: ACM,POJ

1004, 1005是水的不能再水的水题,不说也罢。

题目描述

题目链接:POJ 1006

Biorhythms

Time Limit: 1000MS
Memory Limit: 10000K

Total Submissions: 67775
Accepted: 19911

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 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.

Source

East Central North America 1999

解题分析

换一种表述形式:

设答案为answer;R1,R2,R3是整数;则有:

p+23\dot{}R_{1}=d+answer

e+28\dot{}R_{2}=d+answer

i+33\dot{}R_{3}=d+answer

设n = d + answer,有

\quad p\equiv n\pmod{23}

\quad e\equiv n\pmod{28}

\quad i\equiv n\pmod{33}

至此,这道题转化为一道求解同余方程组的题。

求解过程

详细的数学推导过程用博客来写太麻烦了,这里提供一份用Latex写的详细推导过程(pdf版)

顺便附上一些资料的链接:

Modular Arithmetic
Linear Congruence Theorem
Chinese Remainder Theorem
Jianing Yang关于POJ 1006的分析

代码

int GetAnswer(int p, int e, int i, int d);

int GetAnswer(int p, int e, int i, int d)
{
	const int MAX = 21252;
	const int PERIOD1 = 23, PERIOD2 = 28, PERIOD3 = 33;

	const int R1 = PERIOD2 * PERIOD3 * 6;
	const int R2 = PERIOD1 * PERIOD3 * 19;
	const int R3 = PERIOD1 * PERIOD2 * 2;

	int result = ((R1 * p + R2 * e + R3 * i) % MAX - d + MAX) % MAX;
	if(result == 0)
		return MAX;
	else
		return result;
}

#include <iostream>

using namespace std;

int main()
{
	int p, e, i, d;
	int id = 0;
	do
	{
		cin >> p >> e >> i >> d;
		if(p == -1 && e == -1 && i == -1 && d == -1)
			break;

		id++;
		cout << "Case " << id << ": the next triple peak occurs in " << GetAnswer(p, e, i, d) << " days." << endl;
	}
	while(true);
	return 0;
}

总结

初一看题,是韩信点兵这种题,就直接用小学奥数的方法做了。囧

后来写总结的时候,觉得不能这么草率。中国剩余定理并不是一眼就能看出其正确性的定理,因此我想到了进行推导,没想到因此受益良多:)

基本上找到了点当初学数论的感觉,重新推导了一下辗转相除法,利用辗转相除法证明了扩展欧几里得定理,温习了一下同余定理,认识了一些新的数论理论,比如说贝祖定理,线性同余理论……

posted @ 2010-07-12 01:25  HCOONa  阅读(443)  评论(0编辑  收藏  举报