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.
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 formdays
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.
题目大意
题干有点长,大概的意思是说,人的体内有三个周期:身体周期,情感周期,智力周期,分别对应的长度是23天,28天和33天,也就是说存在一个大的周期使得三个循环出现在同一天,把这一天称为三个高峰点,现在给出某一年中三个周期出现的天数和从这一年开始算起的第n天,求这天之后再次出现三个高峰的天数。
举个栗子:
283 102 23 320表示从第283天、102天、23天分别出现了身体周期、情感周期、智力周期的高峰点,要求的是第320天后首次出现三个高峰点的天数。
题目分析
好不容易看懂了题意,这题的思路却不是很好理清。
其实把题目抽象来看,设三个高峰点出现的天数分别是x1,x2,x3,那么下一次身体周期高峰出现的时间是第x1+23天,下一次情感周期高峰出现在x2+28天,而智力周期高峰在第x3+33天,类推下去的话,第n次单个高峰出现的时间可以表示成23n+x1, 28n+x1, 33n+x3。这也就表面,如果有一天X是三个高峰同时出现的时候,那么X一定同时满足上面三个式子,也就是说:
X % 23 = x1
X % 28 = x2
X % 33 = x3
这其实就是中国剩余定理的表示方法,我们要做的其实就是解开这个同余方程得到X的值,这里利用拓展欧几里得算法和中国剩余定理解决问题。借用了一下雪岩聚聚写的模板解决问题。
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> typedef long long LL; LL qmodx(LL a,LL b,LL c) { a%=c,b%=c; if(c<=1000000000) return a*b%c; return (a*b-(LL)(a/(long double)c*b+1e-8)*c+c)%c;//a*b%c=a*b-floor(a*b/mod)*mod }//O(1)快速乘取模问题 LL exgcd(LL a,LL b,LL &x,LL &y) //ax+by=d { if(!b) { x=1; y=0; return a; } else { LL r=exgcd(b,a%b,x,y); LL t = x; x = y; y=t-a/b*x; return r; } } LL CRT(LL a[],LL m[],LL len) //x%m[i]=a[i] { LL i,x,y,M,n=1,ret=0; for(i=0; i<len; ++i) n*=m[i]; for(i=0; i<len; ++i) { M=n/m[i]; exgcd(M,m[i],x,y);//求逆元 ret=(ret+qmodx(qmodx(x,M,n),a[i],n))%n; } return (ret+n)%n; } int main(int argc, char const *argv[]) { LL len = 3, a[3], b[3] = {23,28,33},d; int count = 0; while(scanf("%lld %lld %lld %lld", &a[0], &a[1], &a[2],&d) != EOF){ if(a[0] == a[1] && a[1] == a[2] && a[2]==d && d == -1) break; a[0]%=23; a[1]%=28; a[2]%=33; count++; LL ans = CRT(a,b,len) - d; if(ans<=0) ans+=21252; printf("Case %d: the next triple peak occurs in %lld days.\n", count, ans); } return 0; } |
总结
第一次自主做出来这种数学性比较强的问题,中国剩余定理在ACM里的应用其实是非常广泛的,再加上快速乘和拓展欧几里得,这几个东西一定得掌握一个。