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: 根据中国剩余定理

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

\left\{ \begin{matrix} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \vdots \qquad\qquad\qquad \\ x \equiv a_n \pmod {m_n} \end{matrix} \right.

[编辑]克罗内克符号

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

\zeta_{i,j} =\begin{cases} 1, \; i=j \\ 0, \; i \ne j \end{cases}

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

  • 对每个1 \le i \le k,先求出正整数bi满足b_i \equiv \zeta_{i,j} \pmod {m_j},即所求的bi满足条件:b_i \equiv 1 \pmod {m_i},但被每个m_j(i\ne j)整除。其求法如下:记r_i =m_{1} \cdots m_{i-1} m_{i+1} \cdots m_k,根据条件m_1 ,\cdots,m_k两两互素,可知rimi也互素,故存在整数cidi使得rici + midi = 1.令bi = rici,则对每个i \ne j,相对应的mj显然整除bi,并且b_i \equiv 1 \pmod {m_i}。由此表明bi即为所求。
  • 对于前述所求的bi,令x_0 = \sum_{i=1}^k a_ib_i,则x_0 \equiv a_i b_i \equiv a_i \pmod {m_i},这说明x0为上述同余方程组的一个解,从而所有的解可表示为x = x_0 + n \prod_{i=1}^k m_i,其中的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;
    	}
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值