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
关键:解法中的三个关键数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;
}
973



被折叠的 条评论
为什么被折叠?



