Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 91042 | Accepted: 27732 |
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.
Source
這裏我們針對題目介紹一下中國剩餘定理的核心思想:
實際上方程的情況可以推廣解決所有的問題。這裏我們爲了是大家對中國剩餘定理更有感覺,我們按照題意給出三個等式:
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*LCM(b,c);
n2=x2*LCM(a,c);
n3=x3*LCM(a,b);
假如說方程有解。
必然存在對應的:k1,k2,k3 使:
k1*LCM(b,c) % a = 1;
k2*LCM(a,c) % b = 1;
k3*LCM(a,b) % c = 1;
那麼我們取:
n1=p1*k1*LCM(b,c) % a = p1;
n2=p2*k2*LCM(a,c) % b = p2;
n3=p3*k3*LCM(a,b) % c = p3;
那麼我們就得出構造的一個結果:
sum=n1+n2+n3=p1*k1*LCM(b,c)+p2*k2*LCM(a,c)+p3*k3*LCM(a,b);
很明顯週期爲:LCM(a,b,c);
最小非負整數解即是:res=sum % LCM(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;
}