POJ 1006 Biorhythms: 孙子定理

Biorhythms

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 90272

Accepted: 27473

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

0 0 0 0

0 0 0 10

05 20 34 32

54 5 6 72

83 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

East Central North America 1999

这是一道考察孙子定理的经典题目。

要想解这道题目就必须先要理解孙子定理是什么

孙子定理呢,它有另一个名字叫中国剩余定理

因为这个算法最先在《孙子算经》里提出,所以就给了它孙子定理这个名字

《孙子算经》里面是这样写的:

有物不知其数,三个一数余二,五个一数余三,七个一数又余二,问该物总数几何

说的是有一个什么东西,不知道有多少,三个三个数,剩了两个,五个五个数剩了三个,七个七个数剩了两个,问这东西有多少个。

正规解法是这样的:

5*7*1+3*7*3+3*5*2=128

128-105=23

所以最小满足题意的数为23

5*7*n说的是用57的倍数乘以一个合适的n值使得5*7*n除以325*7=3535除以3刚好余2,所以n=1

3*7*3和上面一样3*7=2121除以51,为了让它除以53,就把这个式子扩大三倍(余数随之扩大三倍,这个很好理解)

3*5*2以此类推

再把这三个加起来就得到了128

比它们的最小公倍数105大,所以128-105就得到了最小的答案23(23也是128105的余数)

当然《孙子算经》里的这题有更简便的算法:37余数同为2,所以用3*7+2=23,恰好23除以53(完全碰运气......

孙子定理的证明以及各种同余问题,数学控们可以自行百度或谷歌,下面来讲POJ这道题

输入会给4个数p,e,i,d

p,e,i就是这三个对应的余数了(p<23,e<28,i<33时可以这样说),现在关键的问题就是如何制造这些余数呢?而且还是乘法。我们使用的仍然是余数扩大的思想,想想要是余数原本为1,再乘以各种p,e,i余数不久成了p,e,i了吗?(显然p>=23,e>=28,i>=33时也成立)

那问题就转化成了怎么制造余数1.

对于23: 28*33 = 924, 924除以234,立马就想到4*6=24,24除以231

所以第一个和式就是28*33*6*p

对于28: 23*33 = 759, 759除以283,3不可能乘以什么数产生29,所以就想到57 (28*2+1=57),所以就成19

所以推得第二个和式就是23*33*19*e

对于33同理,第三个和式是23*28*2*i

由于题目是要求在d的多少天后,所以把和式减去d后在对23*28*33求余

于是最终算式就是

res = (28*33*6*p+23*33*19*e+23*28*2*i-d)%(23*28*33)

化的更简:

res = (5544*p+14421*e+1288*i-d)%21252

这时可能或出现一种情况:p=e=i=0d不等于0,此时res<0

所以要进行判断.

C Code:

#include <stdio.h>

int main()
{
	int p,e,i,d,n=0,res;
	freopen("1006.in","r",stdin);
	freopen("1006.out","w",stdout);
	
	while(scanf("%d%d%d%d",&p,&e,&i,&d) && p!=-1 && e!=-1 && i!=-1 && d !=-1) {
		n++;
		res=(28*33*6*p+23*33*19*e+23*28*2*i-d)%(23*28*33);
		if(res<=0) printf("Case %d: the next triple peak occurs in %d days.\n",n,21252-d);
		else printf("Case %d: the next triple peak occurs in %d days.\n",n,res);
	}
	
	return 0;
}


 

这里有一些技巧可以总结一下,while判断中写scanf()是比较灵巧的写法,注意scanf()是有返回值的,返回的是接受成功变量的个数,要是一个都没成功,就是0,还有一个返回值表示文件末尾:EOF(EOF就是-1)

这题也可以用暴力方法枚举,这里就不提了,有兴趣可以自己研究,因为枚举也是有技巧的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值