poj 1006 Biorhythms 生理週期

問題
能枚舉嗎?

Biorhythms
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 102540   Accepted: 31642

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.




生理週期
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 102540 Accepted: 31642

Description

人生來就有三個生理週期,分別爲體力、感情和智力週期,它們的週期長度爲23天、28天和33天。每一個週期中有一天是高峯。在高峯這天,人會在相應的方面表現出色。例如,智力週期的高峯,人會思維敏捷,精力容易高度集中。因爲三個週期的周長不同,所以通常三個週期的高峯不會落在同一天。對於每個人,我們想知道何時三個高峯落在同一天。對於每個週期,我們會給出從當前年份的第一天開始,到出現高峯的天數(不一定是第一次高峯出現的時間)。你的任務是給定一個從當年第一天開始數的天數,輸出從給定時間開始(不包括給定時間)下一次三個高峯落在同一天的時間(距給定時間的天數)。例如:給定時間爲10,下次出現三個高峯同天的時間是12,則輸出2(注意這裏不是3)。

Input

輸入四個整數:p, e, i和d。 p, e, i分別表示體力、情感和智力高峯出現的時間(時間從當年的第一天開始計算)。d 是給定的時間,可能小於p, e, 或 i。 所有給定時間是非負的並且小於365, 所求的時間小於21252。 當p = e = i = d = -1時,輸入數據結束。

Output

從給定時間起,下一次三個高峯同天的時間(距離給定時間的天數)。 採用以下格式: Case 1: the next triple peak occurs in 1234 days. 注意:即使結果是1天,也使用複數形式“days”。

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.
微笑


解答

這個問題源自於我國數學古書《孫子算經》中的一道問題:“今有物,不知其數,三三數之,剩二;五五數之,剩三;七七數之,剩二。問物幾何?”意思是一個整數除以三餘二,除以五餘三,除以七餘二,求這個整數(滿足條件且最小)。

做法是:

 n%3=2,n%5=3,n%7=2且3,5,7互質
 找到使(5×7)的倍數模3得1的數,答案是70
 找到使(3×7)的倍數模5得1的數,答案是21
 找到使(3×5)的倍數模7得1的數,答案是15
 那麼(70×2+21×3+15×2) % (3×5×7) = 23,23就是最終答案了

理解如下:

70×2 = 140,當中的2是指n%3 = 2的2,因爲70%3=1,乘2後使其能滿足%3=2的條件,同理,
21×3使其能滿足%5=3
15×2使其能滿足%7=2
又70能整除5和7,21能整除3和7,15能整除3和5
因此70×2+21×3+15×2 = 233能滿足%3=2,%5=3,%7=2的條件
將233對3×5×7=105取模,是爲了取得最小的情況,因爲105是能夠同時整除3,5,7的數,如果減去,對該數滿足%3=2,%5=3,%7=2沒有影響。因此對105取模,得到的結果便是最小並滿足條件的數

拓展開來問題就是有同餘方程組 a = ai (mod ni), 求未知數a。方法是:

定義 n=n1*n2...nk, 其中因子兩兩互質
mi = n1*n2*...nk / ni;   
ci = mi(mf  mod ni);   
其中 mi*mf  mod ni = 1;
則 a = (a1*c1+a2*c2+...+ak*ck) (mod n) 

中國剩餘定理關鍵是mf的求法,如果理解了擴展歐幾里得 ax+by=d, 就可以想到:

          mi*mf  mod ni = 1 => mi*mf+ni*y=1;

這裏的中國剩餘定理必須要求除數是互質的,但是有些題目的同餘方程式除數並不互質,那麼將不能使用傳統的中國剩餘定理






中國剩餘定理:   三週期

   註釋:三數爲a b c,餘數分別爲 m1 m2 m3%爲求餘計算,&&運算

分別找出能被兩個數整除,而滿足被第三個整除餘一的最小的數。

k1%b==k1%c==0 &&k1%a==1;

k2%a==k2%c==0 &&k2%b==1;

k3%a==k3%b==0 &&k3%c==1;

將三個數(能被兩個數整除、除以第三個數餘1)乘對應數字的餘數再加起來,減去這三個數的最小公倍數即得結果。

Answer = k1×m1 + k2×m2 + k3×m3 - P×a×b×c);

P爲滿足Answer > 0的最大整數;

或者 Answer = (k1×m1 + k2×m2+ k3×m3%(a×b×c) ;






同樣,這道題的解法就是: 

已知(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 <iostream>

using namespace std;

int yuyi(int e,int i,int p)
{
    int a,j=2;
    a=e*i;
    while(a%p!=1)
    {
        a=a/(j-1)*j;
        j++;
    }
    return a;
}

int main()
{
    int p,e,i,d,a,b,c,n,j=0;
    while(cin>>p>>e>>i>>d)
    {
        if(p==-1 && e==-1 && i==-1 && d==-1)
            break;
        j++;
        a=b=c=0;
        a=yuyi(28,33,23);
        b=yuyi(23,33,28);
        c=yuyi(23,28,33);
        n=(a*p+b*e+i*c)%(23*28*33)-d;
        while(n<=0)
        {
            n=n+23*28*33;
        }
        cout<<"Case "<<j<<": the next triple peak occurs in "<<n<<" days."<<endl;
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章