中国剩余定理

来源:互联网 发布:哈鲁留学怎么样 知乎 编辑:程序博客网 时间:2024/06/01 22:03
G - 中国剩余定理
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit Status

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.
解释:

8中国剩余定理编辑

在中国古代劳动人民中,长期流传着“隔墙算”、“剪管术”、“秦王暗点兵”等数学游戏。有一首“孙子歌”,甚至远渡重洋,输入日本:
“三人同行七十稀,五树梅花廿一枝,
七子团圆正半月,除百零五便得知。”
这些饶有趣味的数学游戏,以各种不同形式,介绍世界闻名的“孙子问题”的解法,通俗地反映了中国古代数学一项卓越的成就。“孙子问题”在现代数论中是一个一次同余问题,它最早出现在中国公元四世纪的数学著作《孙子算经》中。《孙子算经》卷下“物不知数”题说:有物不知其数,三个一数余二,五个一数余三,七个一数又余二,问该物总数几何?显然,这相当于求不定方程组
N=3x+2,N=5y+3,N=7z+2
的正整数解N,或用现代数论符号表示,等价干解下列的一次同余组。
《孙子算经》所给答案是N=23。由于孙子问题数据比较简单,这个答数通过试算也可以得到。但是《孙子算经》并不是这样做的。“物不知数”题的术文指出解题的方法多三三数之,取数七十,与余数二相乘;五五数之,取数二十一,与余数三相乘;七七数之,取数十五,与余数二相乘。将诸乘积相加,然后减去一百零五的倍数。列成算式就是:
N=70×2+21×3+15×2-2×105。
这里105是模数3、5、7的最小公倍数,容易看出,《孙子算经》给出的是符合条件的最小正整数。对于一般余数的情形,《孙子算经》术文指出,只要把上述算法中的余数2、3、2分别换成新的余数就行了。以R1、R2、R3表示这些余数,那么《孙子算经》相当于给出公式
N=70×R1+21×R2+15×R3-P×105(p是整数)。
解答:
#include<stdio.h>int main(){    long long  a[10],d;    long long  i,t,j,s;    long long  b=23*28*33;//运用孙子定理的前期,所有除数的总积。    long long f[3];    f[0]=23;f[1]=28;f[2]=33;//除数    int q=0;    while(scanf("%I64d%I64d%I64d%I64d",&a[0],&a[1],&a[2],&d)!=EOF)//余数{            q++;            if(a[0]==-1&&a[1]==-1&&a[2]==-1&&d==-1)                break;                s=0;            for(i=0;i<3;i++)            {                t=b/f[i];                if(t%f[i]!=0)                for(j=1;(t*j)%f[i]!=1;j++);//判定j值,保证余数为一。                s+=t*a[i]*j;//得数相加            }              s=s-d+21252;//减去已经过的日子            s=s%b;             if(s==0)                    s=21252;//排除特殊情况            printf("Case %d: the next triple peak occurs in %I64d days.\n",q,s);        }        return 0;    }


0 0