现在的位置: 首页 > 综合 > 正文

poj1006

2014年10月06日 ⁄ 综合 ⁄ 共 3277字 ⁄ 字号 评论关闭
Biorhythms

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.

Source

中国剩余定理。
公元前后的《孙子算经》中有“物不知数”问题:“今有物不知其数,三三数之余二 ,五五数之余三 ,七七数之余二,问物几何?”答为“23”。也就是求同余式组x≡2 (mod3),x≡3 (mod5 ),x≡2
(mod7)(式中a≡b (modm)表示m整除a-b )的正整数解。明朝程大位用歌谣给出了该题的解法:“三人同行七十稀,五树梅花廿一枝,七子团圆月正半,除百零五便得知。”即解为x≡2×70+3×21+2×15≡233≡23(mod105)。此定理的一般形式是设m = m1 ,… ,mk 为两两互素的正整数,m=m1,…mk ,m=miMi,i=1,2,… ,k 。则同余式组x≡b1(modm1),…,x≡bk(modmk)的解为x≡M'1M1b1+…+M'kMkbk (modm)。式中M'iMi≡1 (modmi),i=1,2,…,k
。直至18世纪 C.F.高斯才给出这一定理。孙子定理对近代数学如环论,赋值论都有重要影响。 

  孙子问题的解法,以现代的说法,是找出三个关键数70,21,15。解法的意思就是用70乘3除所得的馀数,21乘5除所得的馀数,15乘7除所得的馀数,然後总加起来,除以105的余数就是答案。

  即题目的答案为 70×2+21×3+15×2

  =140+63+30

  =233

  233-2×105=23

  公式:70a+21b+15c-105n

  关键:解法中的三个关键数70,21,15,有何妙用,有何性质呢?首先70是除3余1而5与7都除得尽的数,所以70a是3除除a,而5与7都除得尽的数,21是5除除1,而3与7都除得尽的数,所以21b是5除余b,而3与7除得尽的数。同理,15c是7除余c,3与5除得尽的数,总加起来 70a+21b+15c 是3除余a,5除余b ,7除余c的数,也就是可能答案之一,但可能不是最小的,这数减105(105=3*5*7)仍有这样性质,可以多次减去105而得到最小的正数解。因为28*33
mod 23=4,要使得28*33*x mod 23=1,x可以
等于6,因为6*4 mod 23=1。同理同理,要使23*33*y mod 28=1,y可以取19。23*28*z mod
33=1,z可以取2。

代码如下:

#include<stdio.h>

int main(void) {
    int p;
    int e;
    int i;
    int d;
    int j=0;
    while (scanf("%d%d%d%d", &p, &e, &i, &d), p + 1) {
        j++;
        int day;
        day = (p * 5544 + e * 14421 + i * 1288) % (23 * 28 * 33);
        day -= d;
        if (day <= 0)
            day += 21252;
        printf("Case %d: the next triple peak occurs in %d days.\n", j, day);
    }
    return 0;
}


抱歉!评论已关闭.