您的位置:首页 > 其它

POJ 1006 中国剩余定理

2017-12-08 22:10 337 查看
问题描述

人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。一个周期内有一天为峰值,在这一天,人在对应的方面(体力,情感或智力)表现最好。通常这三个周期的峰值不会是同一天。现在给出三个日期,分别对应于体力,情感,智力出现峰值的日期。然后再给出一个起始日期,要求从这一天开始,算出最少再过多少天后三个峰值同时出现。

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+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;

4000
所以有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>
#include<cstdio>
using namespace std;
int main()
{
int p,e,i,d,a = 0;
int result;
while(scanf("%d%d%d%d",&p,&e,&i,&d))
{
a++;
if(p==-1&&e==-1&&i==-1&&d==-1)
break;
else
{
result = (5544*p+14421*e+1288*i-d+21252)%(21252);
//出现负数的情况,即从给出的出现峰值开始算起,在到起始日期之前就已经出现三个峰值同时出现的情况,那么天数将为负值。
//0 0 0 100 在第0天即出现三个峰值同时出现,但是要从100开始算起,所以再加一个21252周期,为下一次三个峰值一起出现所需的时间。
}
if(result == 0)
//即给出的三个峰值分别出现的时间即是同时出现的时间,且为开始时间。但是题目要求为下一个满足条件的天数,即为完整的一个周期。
result = 21252;
printf("Case %d: the next triple peak occurs in %d days.\n",a,result);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: