Biorhythms

Biorhythms

 

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 95853   Accepted: 29477

 

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.


 分析:

大致题意:http://poj.org/problem?id=1006&lang=zh-CN&change=true

思路:

中国剩余定理

①线性同余形为ax≡b(mod m)的同余式.
m为正整数,a和b为整数,x为变量
如果aa- ≡1(mod m), a- 称为a的模m逆
②如果a和m为互素的整数,m>1,则存在a的模m逆。而且这个a的模m逆是唯一的。(即有小于m的唯一正整数 a- ,它是a的模m逆,且a的任何别的模m逆均和a- 模m同余1)
证:
由定理1及gcd(a,m)=1知有整数s和t,成立:
 sa+tm=1
于是sa+tm=1(mod m)
由于 tm=0(mod m)所以sa ≡1(mod m)
 s为a的模m逆
③中国剩余定理
令m1,m2,..,mn为两两互素的正整数,则同余方程组
x ≡a1(mod m1)
x ≡a2(mod m2)
x ≡an(mod mn)
有唯一的模m=m1m2…mn的解。(即有一个解x,使0≤x<m,且所有其他的解均与次解模m同余)
证明:
要构造一个适合各方程的解,
首先对k=1,2,…,n,令MK=m/mk,即Mk是除mk以外所有模数的乘积。
由于i≠k时,mi和mk没有大于1的公因子,所以gcd(mk,Mk)=1。
从而由定理3知有Mk模mk的逆,整数yk,使得Mkyk=1(mod mk)
要得到合适所有方程的解,令x ≡a1M1y1+a2M2y2+…+anMnyn
现在证明x就是所求的一个解。
由于只要j≠k,就有Mj=0(mod mk)
x的计算式中除第k项以外的各项模mk均同余于0.由于Mkyk ≡1(mod mk),
我们看到,对k=1,2,…,n,均有x ≡ akMkyk ≡ak(mod mk)
所以,x是这n个同余方程的同一解。
④而根据本题题意正好满足中国剩余定理的条件http://hi.baidu.com/lxyzmnwvhokptur/item/723d466493c51b00a0cf0f4e
有(n+d)%23=p;   (n+d)%28=e;   (n+d)%33=i
使33×28×a被23除余1,用33×28×8=5544 
使23×33×b被28除余1,用23×33×19=14421 
使23×28×c被33除余1,用23×28×2=1288 
则有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d
而23、28、33互质,即lcm(23,28,33)= 21252;
所以n=(5544×p+14421×e+1288×i-d)%21252
本题所求的是最小整数解,避免n为负,因此最后结果为n= [n+21252]% 21252
那么最终求解n的表达式就是:

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

代码简单如下:

# include<stdio.h>
int main()
{
    int p,e,i,d;
    int n;
    int cnt=1;
    while((scanf("%d %d %d %d",&p,&e,&i,&d))!=EOF)
    {
        if(p==-1&&e==-1&&i==-1&&d==-1)
            break;
        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",cnt++,n);
    }
    return 0;
}

 

posted on 2012-11-08 22:21  即为将军  阅读(251)  评论(0)    收藏  举报

导航