POJ 1006 Biorhythms 解法探索

在做POJ 上的1006题目时,发现总是做不对,给的测试用例能完全正确,但是总是得到Wrong Anser结果。郁闷之极,搜了下高人提示:中国剩余定理。在看定理的过程中,自己用推到的4元一次方程,成功求出,并且很容易理解。记录下来供大家共享。

题目1006:

Biorhythms
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 83990   Accepted: 25414

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

---------------------------------------------------------------------------
解法1:直接法
从t=i+CYCLE_I*k开始,k不断增大,找到t既能被CYCLE_P除后余p,又能被CYCLE_E除后余e的数。
源代码如下:
import java.util.Scanner;

public class Main {
	public static final int CYCLE_P = 23;
	public static final int CYCLE_E = 28;
	public static final int CYCLE_I = 33;

	public static void main(String[] args) {
		int p, e, i, d;

		StringBuilder buffer = new StringBuilder(512);
		Scanner input = new Scanner(System.in);

		for (int j = 1; true; j++) {
			p = input.nextInt();
			e = input.nextInt();
			i = input.nextInt();
			d = input.nextInt();
			if (p < 0) {
				break;
			}
			int days = getTriplePeak(p, e, i, d);
			buffer.append("Case ").append(j).append(
					": the next triple peak occurs in ").append(days).append(
					" days.\n");
		}

		System.out.println(buffer.toString());
		input.close();
	}

	/**
	 * 返回从current天开始,要到三个都达到顶峰还要等待的天数。
	 * 
	 * @param p
	 * @param e
	 * @param i
	 * @return
	 */
	public static int getTriplePeak(int p, int e, int i, int d) {
		int peak = 0;

		p %= CYCLE_P;
		e %= CYCLE_E;
		i %= CYCLE_I;
		for (peak = i; peak <= d; peak += CYCLE_I)
			;
		for (; peak % CYCLE_P != p || peak % CYCLE_E != e; peak += CYCLE_I)
			;
		return peak - d;
	}
}


解法2:
设下一次三个周期同时到达的天数为t,身体周期(Physical cycle)要经历x次才到达t,情感周期(Emotional cycle)要经历y次才到达t,知识周期( intellectual cycle)要经历z次才能到达t。列出一个四元一次方程组;
23*x + p = t;  -----(1)
28*y + e = t;  -----(2)
33*z + i = t;  -----(3)

(1)式乘以28*33得到:
23*28*33*x + 28*33*p = 28*33*t;   ---(5)
(2) 式乘以23*33得到:
23*28*33*y + 23*33*e = 23*33*t;   ---(6)
(3)式乘以23*28得到:
23*28*33*z + 23*28*i = 23*28*t;   ---(7)

(5)(6)(7)式相加: 化简求得:
t= (23*28*33*(x+y+z)+(28*33*p+23*33*e+23*28*i))/(28*33+23*33+23*28); ----(9)
进一步化简:
t= (23*28*33*(m)+(28*33*p+23*33*e+23*28*i))/(28*33+23*33+23*28); ----(10)  其中m为整数,t为整数;
根据(10)式,编写代码求解。
源程序如下:
import java.util.Scanner;

public class Main {
	public static final int CYCLE_P = 23;
	public static final int CYCLE_E = 28;
	public static final int CYCLE_I = 33;
	public static final int A = CYCLE_E * CYCLE_I;
	public static final int B = CYCLE_P * CYCLE_I;
	public static final int C = CYCLE_P * CYCLE_E;
	public static final int N = A + B + C;
	public static final long COMMON = CYCLE_P * CYCLE_E * CYCLE_I;

	public static void main(String[] args) {
		dealWithInput();
	}

	public static void dealWithInput() {
		int p, e, i, d;

		StringBuilder buffer = new StringBuilder(512);
		Scanner input = new Scanner(System.in);

		for (int j = 1; true; j++) {
			p = input.nextInt();
			e = input.nextInt();
			i = input.nextInt();
			d = input.nextInt();
			if (p < 0) {
				break;
			}
			long days = getTriplePeak(p, e, i, d);
			buffer.append("Case ").append(j).append(
					": the next triple peak occurs in ").append(days).append(
					" days.\n");
		}

		System.out.println(buffer.toString());
		input.close();
	}

	/**
	 * 返回从current天开始,要到三个都达到顶峰还要等待的天数。
	 * 
	 * @param p
	 * @param e
	 * @param i
	 * @return
	 */
	public static long getTriplePeak(int p, int e, int i, int d) {
		long temp = 0, m = 0;

		p %= CYCLE_P;
		e %= CYCLE_E;
		i %= CYCLE_I;

		m = A * p + B * e + C * i;

		for (temp = m; true; temp += COMMON) {
			if (temp % N == 0) {
				long result = temp / N - d;
				if (result > COMMON)
					return result % COMMON;
				else if (result <= 0)
					return result + COMMON;
				else
					return result;

			}
		}
	}
}

解法3: 根据中国剩余定理

一般地,中国剩余定理是指若有一些两两互质的整数 ,则对任意的整数:a1,a2,...an,以下联立同余方程组对模  有公解:

[编辑]克罗内克符号

为了便于表述,对任意的正整数用一个常用函数ζi,j表示,称之为克罗内克符号(Kronecker).定义:

使用该符号,即可给出上述一般同余方程组的求解过程,分两步完成

  • 对每个,先求出正整数bi满足,即所求的bi满足条件:,但被每个整除。其求法如下:记,根据条件两两互素,可知rimi也互素,故存在整数cidi使得rici + midi = 1.令bi = rici,则对每个,相对应的mj显然整除bi,并且。由此表明bi即为所求。
  • 对于前述所求的bi,令,则,这说明x0为上述同余方程组的一个解,从而所有的解可表示为,其中的n可以取遍所有的整数。

    源程序如下:
    import java.util.Scanner;
    
    public class Main {
    	public static final int CYCLE_P = 23;
    	public static final int CYCLE_E = 28;
    	public static final int CYCLE_I = 33;
    	public static final int A = CYCLE_E * CYCLE_I;
    	public static final int B = CYCLE_P * CYCLE_I;
    	public static final int C = CYCLE_P * CYCLE_E;
    	public static final long LCM = CYCLE_P * CYCLE_E * CYCLE_I;
    	private static final int a;
    	private static final int b;
    	private static final int c;
    
    	static {
    		int temp = 0;
    		int ra = A % CYCLE_P, rb = B % CYCLE_E, rc = C % CYCLE_I;
    
    		// get a
    		for (temp = CYCLE_P + 1; true; temp += CYCLE_P) {
    			if (temp % ra == 0) {
    				a = temp / ra;
    				break;
    			}
    		}
    		// get b
    		for (temp = CYCLE_E + 1; true; temp += CYCLE_E) {
    			if (temp % rb == 0) {
    				b = temp / rb;
    				break;
    			}
    		}
    		// get c
    		for (temp = CYCLE_I + 1; true; temp += CYCLE_I) {
    			if (temp % rc == 0) {
    				c = temp / rc;
    				break;
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		int p, e, i, d;
    
    		StringBuilder buffer = new StringBuilder(512);
    		Scanner input = new Scanner(System.in);
    
    		for (int j = 1; true; j++) {
    			p = input.nextInt();
    			e = input.nextInt();
    			i = input.nextInt();
    			d = input.nextInt();
    			if (p < 0) {
    				break;
    			}
    			long days = getTriplePeak(p, e, i, d);
    			buffer.append("Case ").append(j).append(
    					": the next triple peak occurs in ").append(days).append(
    					" days.\n");
    		}
    
    		System.out.println(buffer.toString());
    		input.close();
    	}
    
    	/**
    	 * 返回从current天开始,要到三个都达到顶峰还要等待的天数。
    	 * 
    	 * @param p
    	 * @param e
    	 * @param i
    	 * @return
    	 */
    	public static long getTriplePeak(int p, int e, int i, int d) {
    		p %= CYCLE_P;
    		e %= CYCLE_E;
    		i %= CYCLE_I;
    
    		long peak = a * A * p + b * B * e + c * C * i;
    		peak %= LCM;
    		peak -= d;
    		if (peak <= 0)
    			return peak + LCM;
    		else
    			return peak;
    	}
    }
    


你可能感兴趣的:(POJ 1006 Biorhythms 解法探索)