POJ_1006_Biorhythms(中国剩余定理)

来源:互联网 发布:it heard 编辑:程序博客网 时间:2024/05/29 17:30

Biorhythms
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 103786 Accepted: 32126

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 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 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

East Central North America 1999

题型:数论(中国剩余定理)


题意:

        人的一生中有三个周期循环,分别是身体、情感、智力周期,这三个周期分别是23、28、33天。每当这三个周期的峰值重合的时候,人表现为最佳状态。现在给出这三个周期在当前这一年的起始时间p、e、i,还有一个日期d,求出在d后多少天三个循环会再一次达到峰值。


分析:

首先来了解一下中国剩余定理。

有题曰:今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?

简化一下:已知 n%3=2, n%5=3, n%7=2, 求n。

这怎么求呢?

因为3、5、7互质,

令x = n%3 = 2、y = n%5 = 3、z = n%7 = 2;

使5×7×a被3除余1,有35 × 2 = 70 % 3 = 1,即a = 2;
使3×7×b被5除余1,用21 × 1 = 21 % 5 = 1,即b = 1;
使3×5×c被7除余1,用15 × 1 = 15 % 7 = 1,即c = 1。
那么n = ( 70×x+21×y+15×z )% lcm(3,5,7) = 23 为n的最小解。

所以答案就是23 + i * lcm( 3,5,7 )。


再回到这个题目,

根据题意,我们可以列出下列式子:

                                           ( x + d ) % 23 = p

                                           ( x + d ) % 28 = e

                                           ( x + d ) % 33 = i

求法是相似的,

令28 * 33 * a % 23 = 1,有5544 % 23 = 1;

令23 * 33 * b % 28 = 1,有14421 % 28 = 1;

令23 * 28 * c % 33 = 1,有1288 % 33 = 1;

因为答案要的是最小的天数,

所以n+d = (5544*p+14421*e+1288*i) % lcm(23,28,33);

即:n = (5544*p+14421*e+1288*i-d) % 21252;

防止式子算下来是负数,n = (5544*p+14421*e+1288*i-d+21252) % 21252;

注意当n=0的时候,要输出21252。


代码:

#include<iostream>#include<cmath>#include<cstring>#include<cstdio>using namespace std;int main(){    int p,e,i,d,time=0;    while(1){        time++;        scanf("%d%d%d%d",&p,&e,&i,&d);        if(p==-1&&e==-1&&i==-1&&d==-1)            break;        int n;        n=(5544*p+14421*e+1288*i-d+21252)%21252;        if(n==0){            n=21252;        }        printf("Case %d: the next triple peak occurs in %d days.\n",time,n);    }}


原创粉丝点击