acm-Biorhythms

来源:互联网 发布:日本爱知世博会ppt 编辑:程序博客网 时间:2024/04/30 13:26

Biorhythms

时间限制:1000 ms  内存限制:65535 KB
难度:3
描述

Some peoplebelieve that there are three cycles in a person's life that startthe day he or she is born. These three cycles are the physical,emotional, and intellectual cycles, and they have periods oflengths 23, 28, and 33 days, respectively. There is one peak ineach period of a cycle. At the peak of a cycle, a person performsat his or her best in the corresponding field (physical, emotionalor mental). For example, if it is the mental curve, thoughtprocesses will be sharper and concentration will beeasier. 
Since the three cycles have different periods, the peaks of thethree cycles generally occur at different times. We would like todetermine when a triple peak occurs (the peaks of all three cyclesoccur in the same day) for any person. For each cycle, you will begiven the number of days from the beginning of the current year atwhich one of its peaks (not necessarily the first) occurs. You willalso be given a date expressed as the number of days from thebeginning of the current year. You task is to determine the numberof days from the given date to the next triple peak. The given dateis not counted. For example, if the given date is 10 and the nexttriple peak occurs on day 12, the answer is 2, not 3. If a triplepeak occurs on the given date, you should give the number of daysto the next occurrence of a triple peak. 

输入
You will be given at most 1000000 cases. The input for each caseconsists 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 currentyear at which the physical, emotional, and intellectual cyclespeak, respectively. The value d is the given date and may besmaller than any of p, e, or i. All values are non-negative and atmost 365, and you may assume that a triple peak will occur within21252 days of the given date. The end of input is indicated by aline in which p = e = i = d = -1
输出
For each test case, print the number of days to the next triplepeak, in the form: 
样例输入
0 0 0 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 301 203 40-1 -1 -1 -1
样例输出
21252211521957516994891010789
来源
POJ

 

解题思路:

中国剩余定理,本题难点不在编程,而是分析题目并转化为数学公式

要引入本题解法,先来看一个故事 “韩信点兵”:
      传说西汉大将韩信,由于比较年轻,开始他的部下对他不很佩服。有一次阅兵时,韩信要求士兵分三路纵队,结果末尾多2人,改成五路纵队,结果末尾多3人,再改成七路纵队,结果又余下2人,后来下级军官向他报告共有士兵2395人,韩信立即笑笑说不对(因2395除以3余数是1,不是2),由于已经知道士兵总人数在2300~2400之间,所以韩信根据23,128,233,------,每相邻两数的间隔是105(3、5、7的最小公倍数),便立即说出实际人数应是2333人(因2333=128+20χ105+105,它除以3余2,除以5余3,除以7余2)。这样使下级军官十分敬佩,这就是韩信点兵的故事。 

 

 韩信点兵问题简化:已知 n%3=2, n%5=3,  n%7=2, 求n。 


  再看我们这道题,读入p,e,i,d 4个整数

已知(n+d)#=p;  (n+d)(=e;  (n+d)3=i ,求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的最小解

 而韩信已知士兵人数在2300~2400之间,所以只需要n+i×lcm(3,5,7)就得到了2333,此时i=22

同样,这道题的解法就是: 

已知(n+d)#=p;  (n+d)(=e;  (n+d)3=i 
       使33×28×a被23除余1,用33×28×8=5544; 
       使23×33×b被28除余1,用23×33×19=14421; 
       使23×28×c被33除余1,用23×28×2=1288。 
      因此有(5544×p+14421×e+1288×i)%lcm(23,28,33) =n+d 

又23、28、33互质,即lcm(23,28,33)= 21252;
      所以有n=(5544×p+14421×e+1288×i-d)!252

本题所求的是最小整数解,避免n为负,因此最后结果为n= [n+21252]% 21252
那么最终求解n的表达式就是:

n=(5544*p+14421*e+1288*i-d+21252)!252;

当问题被转化为一条数学式子时,你会发现它无比简单。。。。直接输出结果了。

代码:

#include
#include
#include
using namespace std;
int main()
{
 int p,e,i,d;
 //ifstream cin("test.txt");
 while(scanf("%d %d %d%d",&p,&e,&i,&d))//cin>>p>>e>>i>>d)
 {
  if(p==-1&&e==-1&&i==-1&&d==-1)
   break;
  int lcm=21252;
  intn=(5544*p+14421*e+1288*i-d+21252)!252;
  if(n<=0) n+=21252;
  printf("%d\n",n);//cout<<n<<endl; 
 
 return 0;
}

原创粉丝点击