poj 1006:Biorhythms(水題,經典題,中國剩余定理)


Biorhythms
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 110991   Accepted: 34541

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

  
  水題,中國剩余定理,經典題
  題意
  人自出生起就有體力,情感和智力三個生理周期,分別為23,28和33天。一個周期內有一天為峰值,在這一天,人在對應的方面(體力,情感或智力)表現最好。通常這三個周期的峰值不會是同一天。現在給出三個日期,分別對應於體力,情感,智力出現峰值的日期。然后再給出一個起始日期,要求從這一天開始,算出最少再過多少天后三個峰值同時出現。
  思路
  這道題需要用到中國剩余定理,中國剩余定理,也稱孫子定理。這里有一個經典問題:
《孫子算經》中的題目:有物不知其數,三個一數余二,五個一數余三,七個一數又余二,問該物總數幾何?
  意思就是有一個數除以3余2,除以5余3,除以7余2,問這個數是多少。
  而孫子定理(中國剩余定理)就是求解這個問題的方法。
  再看這道題,你要求多少天之后,出現三重峰,即要求的天數da要求加上開始的天數d除以三個生理周期23,28,33都是整除。我們將da+d看成一個數n,即n%k1=0,n%k2=0,n%k3=0。那么我們只要把n求出來,就能求出da(d已知)。
  這樣直接套用中國剩余定理的解法即可。
  詳細的過程就不寫了,可以猛戳右方鏈接,說的很詳細:POJ1006-Biorhythms
  對中國剩余定理還不是很懂的可以見百度百科的介紹,寫的不錯:中國剩余定理 - 百度百科
  代碼
 1 #include <iostream>
 2 
 3 using namespace std;  4 int main()  5 {  6     int p,e,i,d,Case=1;  7     while(cin>>p>>e>>i>>d){  8         if(p==-1 && e==-1 && i==-1 && d==-1) break;  9         int da = (5544*p+14421*e+1288*i-d+21252)%21252; 10         if(da==0) 11             cout<<"Case "<<Case++<<": the next triple peak occurs in 21252 days."<<endl; 12         else
13             cout<<"Case "<<Case++<<": the next triple peak occurs in "
14             <<da<<" days."<<endl; 15  } 16     return 0; 17 }

 

Freecode : www.cnblogs.com/yym2013


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM