中国剩余定理

PKU 1006 Biorhythms  

2012-04-15 11:02:34|  分类: 数论|字号 订阅

中国剩余定理

题目大意:人的身体,情感,智力的高峰低谷都由周期,分别是23天,28天和33天,现在给出身体,情感,智力的起始天,请计算由此天开始的第几天会达到三个方便的峰值,输出此峰值。

算法分析:

思路:运用中国剩余定理解得基准数,次数再减去起始天D,再加上232833的最小公倍数21252,其值就是答案。

知识点:
中国剩余定理介绍

在《孙子算经》中有这样一个问题:“今有物不知其数,三三数之剩二(除以32),五五数之剩三(除以53),七七数之剩二(除以72),问物几何?”这个问题称为“孙子问题”,该问题的一般解法国际上称为“中国剩余定理”。

具体解法分三步:

  1. 找出三个数:从35的公倍数中找出被7除余1的最小数15,从37的公倍数中找出被5除余1的最小数21,最后从57的公倍数中找出除31的最小数70
  2. 15乘以22为最终结果除以7的余数),用21乘以33为最终结果除以5的余数),同理,用70乘以22为最终结果除以3的余数),然后把三个乘积相加(15*2+21*3+70*2)得到和233
  3. 233除以357三个数的最小公倍数105,得到余数23,即233%105=23。这个余数23就是符合条件的最小数。

就这么简单。我们在感叹神奇的同时不禁想知道古人是如何想到这个方法的,有什么基本的数学依据吗?

中国剩余定理分析

我们将“孙子问题”拆分成几个简单的小问题,从零开始,试图揣测古人是如何推导出这个解法的。

首先,我们假设n1是满足除以32的一个数,比如258等等,也就是满足3*k+2k>=0)的一个任意数。同样,我们假设n2是满足除以53的一个数,n3是满足除以72的一个数。

有了前面的假设,我们先从n1这个角度出发,已知n1满足除以32,能不能使得 n1+n2 的和仍然满足除以32?进而使得n1+n2+n3的和仍然满足除以32

这就牵涉到一个最基本数学定理如果有a%b=c,则有(a+kb)%b=c(k为非零整数),换句话说,如果一个除法运算的余数为c,那么被除数与k倍的除数相加(或相减)的和(差)再与除数相除,余数不变。这个是很好证明的。

以此定理为依据,如果n23的倍数,n1+n2就依然满足除以32。同理,如果n3也是3的倍数,那么n1+n2+n3的和就满足除以32。这是从n1的角度考虑的,再从n2n3的角度出发,我们可推导出以下三点:

  1. 为使n1+n2+n3的和满足除以32n2n3必须是3的倍数。
  2. 为使n1+n2+n3的和满足除以53n1n3必须是5的倍数。
  3. 为使n1+n2+n3的和满足除以72n1n2必须是7的倍数。

因此,为使n1+n2+n3的和作为“孙子问题”的一个最终解,需满足:

  1. n1除以32,且是57的公倍数。
  2. n2除以53,且是37的公倍数。
  3. n3除以72,且是35的公倍数。

所以,孙子问题解法的本质是从57的公倍数中找一个除以32的数n1,从37的公倍数中找一个除以53的数n2,从35的公倍数中找一个除以72的数n3,再将三个数相加得到解。在求n1n2n3时又用了一个小技巧,以n1为例,并非从57的公倍数中直接找一个除以32的数,而是先找一个除以31的数,再乘以2

这里又有一个数学公式如果a%b=c,那么(a*k)%b=a%b+a%b+…+a%b=c+c+…+c=kck>0),也就是说,如果一个除法的余数为c,那么被除数的k倍与除数相除的余数为kc展开式中已证明。

最后,我们还要清楚一点,n1+n2+n3只是问题的一个解,并不是最小的解。如何得到最小解?我们只需要从中最大限度的减掉掉357的公倍数105即可。道理就是前面讲过的定理“如果a%b=c,则有(a-kb)%b=c”。所以(n1+n2+n3)%105就是最终的最小解。

总结

经过分析发现,中国剩余定理的孙子解法并没有什么高深的技巧,就是以下两个基本数学定理的灵活运用:

  1. 如果 a%b=c,则有 (a+kb)%b=c (k为非零整数)。
  2. 如果 a%b=c,那么 (a*k)%b=kc (k为大于零的整数)。
代码如下:

 #include<iostream> using namespace std; int main(){  int p,e,i,d,j,k,a=1,b=1,c=1;  for(j=1;;j++){   if(23*28*j%33==1){    a=23*28*j;  break;   }  }  for(j=1;;j++){   if(28*33*j%23==1){    b=28*33*j;    break;   }  }  for(j=1;;j++){   if(23*33*j%28==1){    c=23*33*j;    break;   }  }  j=0;  printf("a=%d\tb=%d\tc=%d\n",a,b,c);  while(scanf("%d%d%d%d",&p,&e,&i,&d)==4 && !(p==-1 && e==-1 && i==-1 && d==-1)){   j++;   k=(i*a+p*b+e*c-d+21252)%(23*28*33);   if(k>0)    printf("Case %d: the next triple peak occurs in %d days.\n",j,k);   else    printf("Case %d: the next triple peak occurs in 21252 days.\n",j);  }  return 0; }

下面为加强版:

/*#include <iostream> using namespace std; int main() {  int p,e,i,d,t = 0, n;  while( cin >> p >> e >> i >> d && p != -1 )  {   n = (5544*p + 14421*e + 1288*i) % 21252 - d;   if( n <= 0)    n += 21252;   printf("Case %d: the next triple peak occurs in %d days.\n",++t,n);  }  return 0; } */ #include <iostream> using namespace std; int main() {  int p,e,i,d,t = 0, n;  while( cin >> p >> e >> i >> d && p != -1 )  {   for(n = d+1;;n++)   {    if( (n-p)%23 == 0 && (n-e)%28 == 0 && (n-i)%33 == 0 )    {     printf("Case %d: the next triple peak occurs in %d days.\n",++t,n-d);     break;    }   }  }  return 0; }

 
 
Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 89719 Accepted: 27288

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值