中国剩余定理算法

G - 中国剩余定理
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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.


中国古代求解一次同余式组(见同余)的方法。是数论中一个重要定理。又称中国剩余定理。

中国剩余定理的结论:

令任意固定整数为M,当M/A余a,M/B余b,M/C余c,M/D余d,…,M/Z余z时,这里的A,B,C,D,…,Z为除数,除数为任意自然数(如果为0,没有任何意义,如果为1,在孙子定理中没有计算和探讨的价值,所以,不包括0和1)时;余数a,b,c,d,……,z为自然整数时。

1、当命题正确时,在这些除数的最小公倍数内有解,有唯一的解,每一个最小公倍数内都有唯一的解;当命题错误时,在整个自然数范围内都无解。

2、当M在两个或两个以上的除数的最小公倍数内时,这两个或两个以上的除数和余数可以定位M在最小公倍数内的具体位置,也就是M的大小。

3、正确的命题,指没有矛盾的命题:分别除以A,B,C,D,…,Z不同的余数组合个数=A,B,C,D,…,Z的最小公倍数=不同的余数组合的循环周期.

注释:三数为a b c,余数分别为 m1 m2 m3.

⒈分别找出能被两个数整除,而满足被第三个整除余一的最小的数。(即k1,k2,k3)

k1%b==k1%c==0 && k1%a==1;

k2%a==k2%c==0 && k2%b==1;

k3%a==k3%b==0 && k3%c==1;

⒉将三个数(能被两个数整除、除以第三个数余1)乘对应数字的余数再加起来,减去这三个数的最小公倍数即得结果。

Answer = k1×m1 + k2×m2 + k3×m3 - P×(a×b×c);

P为满足Answer > 0的最大整数;

或者 Answer = (k1×m1 + k2×m2 + k3×m3)%(a×b×c) ;

代码实现如下:

void Getva()
{
	int i;
	for(i=1,r1=28*33;;i++)
		if(r1*i%23==1)
			break;
	r1*=i;
	for(i=1,r2=23*33;;i++)
		if(r2*i%28==1)
			break;
	r2*=i;
	for(i=1,r3=23*28;;i++)
		if(r3*i%33==1)
			break;
	r3*=i;
}

定理的解题思路:

令某数为M,令素数为A,B,C,D,…,Z,已知M/A余a,M/B余b,M/C余c,M/D余d,…,M/Z余z。求M=?

因为A,B,C,D,…,Z为不同的素数,故,B*C*D*…*Z不可能被A整除,有等差数列(B*C*D*…*Z)+(B*C*D*…*Z)N中取A个连续项,这A个连续项分别除以A的余数必然存在0,1,2,3,…,A-1,所以,从这A个连续项中能寻找到除以A余1的数。再用除以A余1的这个数*a其积必然除以A余a,这个除以A余a的数,为能够被素数B*C*D*…*Z整除的数,为第一个数;

再按同样的道理,从A*C*D*…*Z的倍数中寻找除以B余b的数,该数具备被素数A,C,D,…,Z整除的特性,为第二个数;

因为,第一个数除以A余a,第二个数能被素数A,C,D,…,Z整除,即能被A整除,所以,第一个数+第二个数之和,仍然保持除以A余a;

同理,第二个数除以B余b,因第一个数能被B整除,所以,第二个数+第一个数之和,仍然保持除以B余b。即,第一个数+第二个数之和,为满足除以A余a,除以B余b。

依此类推,按上面的方法寻找到除以各素因子的余数的数之总和,为满足除以各素因子余数的条件的数。总和再减去能被这几个素数共同整除的数(A*B*C*D*…*Z)N后,其差仍然保持除以各素因子余数的条件的数。由此构成孙子定理的解法。

证明

令T1=k1×m1,T2=k2×m2,T3=k3×m3;

因为 k1%a==1 ;所以 T1%a==m1;

对于 a,已知:T2%a==0,T3%a==0,T1%a==m1;

所以:Answer%a==m1;

因为:T1%b==0,T3%b==0,T2%b==m2 => Answer%b==m2

同理:Answer%c==m3;

又因为 a×b×c能同时整除 a b c,所以Answer ± P×(a×b×c)也是题目的解;

所以Answer是题目的解,又Answer = Answer % (a×b×c),所以Answer为最小解.


本题全代码:

<pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
int r1,r2,r3,r;
void Getva()
{
	int i;
	for(i=1,r1=28*33;;i++)
		if(r1*i%23==1)
			break;
	r1*=i;
	for(i=1,r2=23*33;;i++)
		if(r2*i%28==1)
			break;
	r2*=i;
	for(i=1,r3=23*28;;i++)
		if(r3*i%33==1)
			break;
	r3*=i;
}

int main()
{
	int p,e,i,d,sum,k=1;
	r=21252;
	Getva();
	while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF && p!=-1)
	{
		sum=(r1*p+r2*e+r3*i-d)%r;
		if(sum<=0)							// 该步保证像第一个这样的特殊样例能过
			sum=(sum+r-1)%r+1;							
		printf("Case %d: the next triple peak occurs in %d days.\n",k++,sum);
	}
	return 0;
}


 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值