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.
中国剩余定理:http://blog.csdn.net/u013476556/article/details/38307335
现设 num 是下一个相同日子距离开始的天数
p,e,i,d 如题中所设!
那么就可以得到三个式子:( num + d ) % 23 == p; ( num + d ) % 28 == e; ( num + d ) % 33 == i;
p,e,i,d 是我们输入的,那么我们需要求出num即可,为了方便,我们将num+d暂时作为一个整体!令x = num + d;即:x % 23 == p; x % 28 == e; x % 33 == i;求x
使 33 * 28 * a % 23 = 1,得a = 6; 33 * 28 * 6 = 5544;
使23 * 33 * b % 28 = 1, 得b = 19;23 * 33 * 19 = 14421;
使23 * 28 * c % 33 = 1, 得c = 2; 23 * 28 * 2 = 1288。
那么x = 5544 * p + 14421 * e + 1288 * i
那么x-d即相差的时间天数!
因为有范围限制,那么(x-d) %= 21252;且如果此时<=0,那么(x-d) += 21252 ,以上都只是为了保证在范围内而已~
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int ma (int x,int y)
{
int i = 0;
for ( i = 0;;i++)
{
if (x * i % y == 1)
break;
}
x = x * i;
return x;
}
int main ()
{
int x1,x2,x3;
x1 = 28 * 33;
x2 = 23 * 33;
x3 = 23 * 28;
int s = 28*33*23;
x1 = ma (x1,23);
x2 = ma (x2,28);
x3 = ma (x3,33);
int a,b,c,d;
int cou = 0;
while (~scanf ("%d%d%d%d",&a,&b,&c,&d))
{
cou++;
if (a == -1 && b == -1 && c == -1 && d == -1)
break;
int sum = x1 * a + x2 * b + x3 * c;
int k = (sum - d + s) % s;
if (k <= 0)
k += s;
printf("Case %d: the next triple peak occurs in %d days.\n",cou,k);
}
return 0;
}