Biorhythms_餘數定理

題目描述

    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.

輸入描述:

    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.

輸出描述:

    Case: the next triple peak occurs in 1234 days.
    Use the plural form "days'' even if the answer is 1.
示例1

輸入

複製
0 0 0 0

輸出

複製
Case: the next triple peak occurs in 21252 days.

//找到三個週期的最小公倍數,然後求出與d的距離

可以直接枚舉,暴力

//即N+d=23*N1+p=28*N2+e=33*N3+i;題目要求求解的是N

找到一個數模23餘數爲p,模28餘數爲e,模33餘數爲i;使用中國剩餘定理

除以23餘1,整除28,整除33的三個條件的最小數,發現是5544。

然後求滿足  除以28餘1,整除23,整除33的三個條件的最小數,是14421。

然後求滿足  除以33餘1,整除23,整除28的三個條件的最小數,是1288。

然後5544*p+14421*e+1288*i就一定是三個週期的下一個巔峯了,只不過不一定是最小值。最後模最小公倍數就是最小值。

#include<iostream>
using namespace std;
int main(){
	int p,e,i,d,N=0;
	int days;
	while(cin>>p>>e>>i>>d){
		if(p==-1&&e==-1&&i==-1&&d==-1)
		break;
		days=(p*5544+e*14421+i*1288)%21252;
		 if(days-d<=0)
   			days+=21252;
		cout<<"Case: the next triple peak occurs in "<<days-d<<" days."<<endl;
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章