⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zju1160.cpp

📁 ZJU/ZOJ 1160 的解和解题方法说明. 程序在ZOJ 排名第一.
💻 CPP
字号:
//: ZJU1160.CPP
/* Biorhythms
http://acm.zju.edu.cn/show_problem.php?pid=1160
--------------------------------------------------------------------------------
Time limit: 10 Seconds   Memory limit: 32768K  
Total Submit: 2766   Accepted Submit: 823  
--------------------------------------------------------------------------------
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 3 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.

This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed
by N input blocks. Each input block is in the format indicated in the problem
description. There is a blank line between input blocks. The output format
consists of N output blocks. There is a blank line between output blocks.


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


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.

--------------------------------------------------------------------------------
Problem Source: East Central North America 1999; Pacific Northwest 1999
--------------------------------------------------------------------------------
因为体力、感情和心智高峰周期分别为 (P,E,I)=(23,28,33), 故三者同时到达高峰的周
期是 L=LCM(P,E,I)=21252. 问题是: 如果今年三者的高峰日分别是今年的第 (p,e,i) 日,
那么从今年的第 d 日起, 经过多少日将到达下一个三者同时到达高峰日.
    问题的解为 (x-d+L)%L, 其中x为同余方程组 x=p(mod P), x=e(mod E), x=i(mod I)
的解. 由孙子定理, x=(E*I*p+I*P*e+P*E*i)*S%L, 其中 S 为 (E*I+I*P+P*E)x=1(mod L)
的解. 下面程序使用模板元编程计算 LCM. 但同余方程 Dx=1(mod L) 的解 S 则是利用
程序中的函数 mod 预先计算的.

*/
// All Rights Reserved by Prinse Wang, Goldwe Tech Corp., Ltd

// 使用行输入. ZJU 0.00s 388K AC(2008-02-24 07:54:10).
#include <stdio.h>

#define Lcm(a,b) LCM<(a),(b)>::v

template<int A, int B>struct GCD { enum { v=GCD<B,A%B>::v }; };
template<int A>struct GCD<A,0> { enum { v=A }; };
template<int A, int B>struct LCM { enum { v=A/GCD<A,B>::v*B }; };

const int P=23, E=28, I=33, L=Lcm(Lcm(P,E),I), D=E*I+I*P+P*E;
const int S=12311/*Dx=1(mod L) 的解*/, A=E*I*S%L, B=I*P*S%L, C=P*E*S%L;

bool b[128];
char buf[20];
int p, e, i, d, l;

//int mod(int a, int n) {
//  int x=1, y=0, x1=0, y1=1, x2, y2, q, r, b=n;
//  while (b>0) {
//    q=a/b; r=a%b; a=b; b=r;
//    x2=x-q*x1; x=x1; x1=x2;
//    y2=y-q*y1; y=y1; y1=y2;
//  }
//	return x<0?x+n:x;
//}

int geti() { int r=0, ch; while(b[ch=buf[l++]])r=10*r+(ch&0xF); return r; }

int main() {
  for (int j='0'; j<='9'; j++) b[j]=1;
  int t, k;
  gets(buf); l=0; t=geti();
  while (t--) {
    k=1; gets(buf);
    while (gets(buf),buf[0]!='-') {
      l=0; p=geti(); e=geti(); i=geti(); d=geti();
      if ((d=(p*A+e*B+i*C-d)%L)<=0) d+=L;
      printf("Case %d: the next triple peak occurs in %d days.\n",k++,d);
    }
    if (t) putchar('\n');
  }
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -