一、题目

Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 83375 Accepted: 25237

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

二、思路
1、暴力搜索从d开始测试满足三个条件的数据;
2、中国剩余定理;
三、测试数据
//注意特殊测试数据
//24 29 34 0           1
//24 29 34 1           21252
//24 29 34 2           21251
//0  0  0  0           21252
四、ac源代码
#include<iostream>
using namespace std;
int main()
{
int p,e,i,d,n,j=0;
while(cin>>p>>e>>i>>d)
{
if(p==-1&&e==-1&&i==-1&&d==-1) break;
n=0;
j++;
while(1)
{
n++;
if((d+n-p)%23==0&&(d+n-e)%28==0&&(d+n-i)%33==0)
break;
}
cout<<"Case "<<j<<": the next triple peak occurs in "<<n<<" days."<<endl;

}
return 0;
}


五、中国剩余定理(摘抄)
   先来看一个故事 
      传说西汉大将韩信,由于比较年轻,开始他的部下对他不很佩服。有一次阅兵时,韩信要求士兵分三路纵队,结果末尾多2人,改成五路纵队,结果末尾多3人,再改成七路纵队,结果又余下2人,后来下级军官向他报告共有士兵2395人,韩信立即笑笑说不对(因2395除以3余数是1,不是2),由于已经知道士兵总人数在2300?/FONT>2400之间,所以韩信根据23,128,233,------,每相邻两数的间隔是105,便立即说出实际人数应是2333人(因2333=128+20χ105+105,它除以3余2,除以5余3,除以7余2)。这样使下级军官十分敬佩,这就是韩信点兵的故事。 

   简化:已知 n%3=2,n%5=3,n%7=2,求n。 
   再看我们这道题,读入p,e,i,d 4个整数,已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n 。 
   是不是一样呢? 

   呵呵,确实一样。想到这里觉得很兴奋。但是韩信是怎么计算出结果的呢? 
   随便google了一下,原来这个东西叫“中国剩余定理”,《孙子算经》中就有计算方法。 
   韩信应该是这样算的: 
      因为n%3=2,n%5=3,n%7=2且3,5,7互质 
      使5×7被3除余1,用35×2=70; 
       使3×7被5除余1,用21×1=21; 
       使3×5被7除余1,用15×1=15。 
      (70×2+21×3+15×2)%(3×5×7)=23 

   同样,这道题也应该是: 
       使33×28被23除余1,用33×28×8=5544; 
       使23×33被28除余1,用23×33×19=14421; 
       使23×28被33除余1,用23×28×2=1288。 
      (5544×p+14421×e+1288×i)%(23×28×33)=n+d 
       n=(5544×p+14421×e+1288×i-d)%(23×28×33)
       由于我们面对的是计算机,所以以上那些很大的数字,可以单独写程序让电脑在近乎0的时候内求出:) 为什么要单独写呢?嘿嘿,为了主程序的效率着想~ 

    程序已经出来了 
 1#include <stdio.h> 
 2void main() 
 3
 4int p,e,i,d,n; 
 5int num=1
 6while(1
 7
 8scanf("%d%d%d%d",&p,&e,&i,&d); 
 9if(p!=-1
10
11n=(5544*p+14421*e+1288*i-d+21252)%21252
12if(n==0)n=21252
13printf("Case %d: the next triple peak occurs in %d days.\n",num++,n); 
14}
 
15else break
16}
 
17
18}
 


posted on 2011-09-22 00:14  小狗狗ACM  阅读(668)  评论(0编辑  收藏  举报