首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】1006 - Biorhythms(CRT)

【POJ】1006 - Biorhythms(CRT)

作者头像
FishWang
发布2025-08-26 15:10:11
发布2025-08-26 15:10:11
9700
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Biorhythms

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 129156

Accepted: 41067

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 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

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: Case 1: the next triple peak occurs in 1234 days. Use the plural form ``days'' even if the answer is 1.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
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

East Central North America 1999

给出的三个数互质,所以直接用中国剩余定理即可。最后注意ans - now <= 0 的情况,加上21252 就行了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
int GCD(int a , int b)
{
	return b == 0 ? a : GCD(b,a%b);
}
void exGCD(int a,int b,int &x,int &y)
{
	if (!b)
	{
		x = 1;
		y = 0;
		return;
	}
	exGCD(b,a%b,y,x);
	y -= a / b * x;
}
int CRT(int *m,int *a,int n)
{
	int M = 1,Mi,ans = 0;
	int x,y;
	for (int i = 1 ; i <= n ; i++)
		M = M / GCD(M , m[i]) * m[i];
	for (int i = 1 ; i <= n ; i++)
	{
		Mi = M / m[i];
		exGCD(m[i],Mi,x,y);
		ans = (ans + a[i] * Mi * y) % M;
	}
	if (ans < 0)
		ans += M;
	return ans;
}
int main()
{
	int Case = 1;
	int m[4] = {0,23,28,33};
	int a[4];
	int now;
	while (~scanf ("%d %d %d %d",&a[1],&a[2],&a[3],&now))
	{
		if (a[1] == -1 && a[2] == -1 && a[3] == -1 && now == -1)
			break;
		int ans = CRT(m,a,3);
		if (ans <= now)
			ans += 21252;		//由样例第一组知道
		ans -= now;
		printf ("Case %d: the next triple peak occurs in %d days.\n",Case++,ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档