POJ Biorhythms 中国剩余定理概念理解入门

来源:互联网 发布:php系统开发教程 编辑:程序博客网 时间:2024/04/29 03:28
 Biorhythms
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 91042 Accepted: 27732

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

这里我们针对题目介绍一下中国剩余定理的核心思想:

实际上方程的情况可以推广解决所有的问题。这里我们为了是大家对中国剩余定理更有感觉,我们按照题意给出三个等式:

x % a=p1

x % b=p2

x % c=p3;

求满足(注意:我们说的都是非负整数的情况,不要想多,数数是数不出负值的)上述等式最小的非负数值(注意:他们喜欢在这个地方卡人玩)。

我现在只讲过程,不讲原因,因为这是种构造的思想,只要最后够造的结果,我们能证明是对的,那么也就是说它是正确的!

1.我们分别找一个元素满足:n1 % a=p1; n2 % b=p2; n3 % c=p3;

且x中有一解: x=n1+n2+n3.

那么由于:

x % a=p1

x % b=p2

x % c=p3;

所以, 我们保证: n2 % a=0, n3 % a=0, n1% b=0,n3 % b=0; n1 % c=0, n2 %c=0;

因此我们可以取:

n1=x1*GCD(b,c);

n2=x2*GCD(a,c);

n3=x3*GCD(a,b);

假如说方程有解。

必然存在对应的:k1,k2,k3 使:

k1*GCD(b,c) % a = 1;

k2*GCD(a,c) % b = 1;

k3*GCD(a,b) % c = 1;

那么我们取:

n1=p1*k1*GCD(b,c) % a = p1;

n2=p2*k2*GCD(a,c) % b = p2;

n3=p3*k3*GCD(a,b) % c = p3;

那么我们就得出构造的一个结果:

sum=n1+n2+n3=p1*k1*GCD(b,c)+p2*k2*GCD(a,c)+p3*k3*GCD(a,b);

很明显周期为:GCD(a,b,c);

最小非负整数解即是:res=sum % GCD(a,b,c);

/*  * File:   main.cpp * Author: hit-acm * * Created on 2012年5月24日, 下午11:24 */#include <iostream>#include <cstdio>using namespace std;long long GCD(long long a, long long b) {    return (b == 0) ? a : GCD(b, a % b);}long long LCM(long long a, long long b) {    return b / GCD(a, b) * a;}int main() {    int a = 23, b = 28, c = 33;    long long ab = LCM(a, b);    long long bc = LCM(b, c);    long long ac = LCM(a, c);    long long abc = LCM(ab, c);    long long ac1, bc1, ab1;    for (long long i = 1; (ab1 = i * ab) % c != 1; i++);    for (long long i = 1; (bc1 = i * bc) % a != 1; i++);    for (long long i = 1; (ac1 = i * ac) % b != 1; i++);    long long p, e, i, d;    int cas = 1;    while (scanf("%lld%lld%lld%lld", &p, &e, &i, &d)) {        if (p == -1 && e == -1 && i == -1 && d == -1) break;        long long n1 = (p % a) * bc1;        long long n2 = (e % b) * ac1;        long long n3 = (i % c) * ab1;        long long sum = n1 + n2 + n3;        long long res = sum % abc;        if (res > 0) {            printf("Case %d: the next triple peak occurs in %lld days.\n", cas++, (abc + res - d % abc) % abc);        } else {            printf("Case %d: the next triple peak occurs in %lld days.\n", cas++, abc - d % abc);        }    }    return 0;}