Biorhythms
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 102540 | Accepted: 31642 |
Description
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
Output
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
Input
Output
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;
}