POJ1006_Biorhythms【中国剩余定理】

Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 113892 Accepted: 35736

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

 
题目大意:
已知三个指数的周期长度为23天、28天和33天,现在给你三个指数出现
高峰的时间和一个给定时间,求下次三个指数高峰同天的时间
思路:
典型的中国剩余定理,设三个指数高峰周期为p,e,i,给定时间为d,所
求时间为n
先来看中国剩余定理(简单描述):
已知 n%3=2,  n%5=3,  n%7=2,  求n
因为n%3=2, n%5=3, n%7=2 且 3,5,7互质 (互质可以直接得到这三
个数的最小公倍数)
令x= n % 3 = 2 , y= n%5=3 ,z= n%7=2 
    使5×7×a被3除余1,有35×2=70,即a=2; 
    使3×7×b被5除余1,用21×1=21,即b=1; 
    使3×5×c被7除余1,用15×1=15,即c=1。 
那么n =(70×x+21×y+15×z)%lcm(3,5,7) = 23 这是n的最小解
其中,a为(5*7)^-1 mod 3  b为(3*7)^-1 mod 5 c为(3*5)^-1 mod 7
再来看本题:
已知(n+d)%23 = p,(n+d)%28 = e,(n+d)%33 = i,其中23、28、33
是互质的,求n
使33×28×a被23除余1 ,使23×33×b被28除余1,使23×28×c被33除余1
利用扩展欧几里得分别求出乘法逆元
a = (n+d)^-1 mod 23,b = (n+d)^-1 mod 28,c = (n+d)^-1 mod 33
得出a = 6,b = 19,c = 2
所以有n + d = (28*33*6*p + 23*33*19*e + 23*28*2*i ) % lcm(23*28*33);
因为所求n为最小整数解,则
n = (28*33*6*p + 23*33*19*e + 23*28*2*i - d + 21252) % 21252;
# include<stdio.h>

int main()
{
	int p,e,i,d,n;
	int kase = 1;
	while(scanf("%d%d%d%d",&p,&e,&i,&d)&&(p!=-1 || e!=-1 || i!= -1 || d!=-1))	
	{
		n = (28*33*6*p + 23*33*19*e + 23*28*2*i - d + 21252) % 21252;
		if(n == 0)
			n = 21252;
		printf("Case %d: the next triple peak occurs in %d days.\n",kase++,n);
	}
	return 0;
} 

 
扩展欧几里得求乘法逆元的代码:
# include<stdio.h>

int e_gcd(int a,int b,int &x,int &y)
{
	if(b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	else
	{
		int r = e_gcd(b,a%b,x,y);
		int t = y;
		y = x - (a/b)*y;
		x = t;
		//printf("%d %d\n",x,y);
		return r;//r是gcd 
	}
}
int mul_inverse_mod(int a,int n,int &x,int &y)
{
	int r = e_gcd(a,n,x,y);
	if(1 % r != 0)
		return -1;
	int ans_x = x/r < 0 ? x/r + n : x/r;
	
	return ans_x; 
}
int main()
{
	int a,n;
	int x,y;
	while(~scanf("%d%d",&a,&n))
	{
		int ans = mul_inverse_mod(a,n,x,y);
		printf("%d %d %d\n",x,y,ans);
	}	
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值