Biorhythms
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 111285   Accepted: 34638

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天。一个周期内有一天为峰值,在这一天,人在相应的方面(体力,情感或智力)表现最好。通常这三个周期的峰值不会是同一天。如今给出三个日期,分别相应于体力,情感,智力出现峰值的日期。然后再给出一个起始日期,要求从这一天開始,算出最少再过多少天后三个峰值同一时候出现。

解题思路:

首先化简一下问题:欲求从某天開始,算出再过多少天后三个峰值同一时候出现,设要求的这个值为ans

假设我们能找到一天,三个峰值同一时候出现,我们记这天为 x ,再记 体力,情感,智力出现峰值的日期 分别为 a,b,c ,起始日期为 d.

那么必定:满足条件 x=a+23*t1=b+28*t2=c+33*t2.

再看一下 ans 与 x 的关系:ans = ( x - d ) % (23*28*33) ,所以求出 x 就能够了。

求X要用到 中国剩余定理

【转】自:点击打开链接

中国剩余定理介绍

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

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

中国剩余定理分析

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

首先,我们如果n1是满足除以3余2的一个数,比方2,5,8等等,也就是满足3*k+2(k>=0)的一个随意数。相同,我们如果n2是满足除以5余3的一个数,n3是满足除以7余2的一个数。

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

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

以此定理为根据,假设n2是3的倍数,n1+n2就依旧满足除以3余2。同理,假设n3也是3的倍数,那么n1+n2+n3的和就满足除以3余2。这是从n1的角度考虑的,再从n2,n3的角度出发,我们可推导出下面三点:

  1. 为使n1+n2+n3的和满足除以3余2,n2和n3必须是3的倍数。
  2. 为使n1+n2+n3的和满足除以5余3,n1和n3必须是5的倍数。
  3. 为使n1+n2+n3的和满足除以7余2,n1和n2必须是7的倍数。

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

  1. n1除以3余2,且是5和7的公倍数。
  2. n2除以5余3,且是3和7的公倍数。
  3. n3除以7余2,且是3和5的公倍数。

所以,孙子问题解法的本质是从5和7的公倍数中找一个除以3余2的数n1,从3和7的公倍数中找一个除以5余3的数n2,从3和5的公倍数中找一个除以7余2的数n3,再将三个数相加得到解。在求n1,n2,n3时又用了一个小技巧,以n1为例,并不是从5和7的公倍数中直接找一个除以3余2的数,而是先找一个除以3余1的数,再乘以2。

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

最后,我们还要清楚一点,n1+n2+n3仅仅是问题的一个解,并非最小的解。怎样得到最小解?我们仅仅须要从中最大限度的减掉掉3,5,7的公倍数105就可以。道理就是前面讲过的定理“假设a%b=c,则有(a-kb)%b=c”。所以(n1+n2+n3)%105就是终于的最小解。

解题代码:

#include <iostream>
#include <cstdio>
using namespace std; int a,b,c,d;
int n1,n2,n3; void get(){//get n1,n2,n3
for(int i=1;;i++){
if(28*33*i%23==1){
n1=28*33*i;
break;
}
}
for(int i=1;;i++){
if(23*33*i%28==1){
n2=23*33*i;
break;
}
}for(int i=1;;i++){
if(23*28*i%33==1){
n3=23*28*i;
break;
}
}
} int main(){
get();
int casen=0;
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
if( a==-1 && b==-1 && c==-1 && d==-1) break;
int ans=(n1*a+n2*b+n3*c-d)%(23*28*33);
ans=ans<=0?ans+23*28*33:ans;
printf("Case %d: the next triple peak occurs in %d days.\n",++casen,ans);
}
return 0;
}

POJ 1006 Biorhythms (数论-中国剩余定理)的更多相关文章

  1. poj 1006 Biorhythms (中国剩余定理模板)

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  2. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

  3. POJ 1006 生理周期(中国剩余定理)

    POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...

  4. POJ 1006 Biorhythnms(中国剩余定理)

    http://poj.org/problem?id=1006 题意: (n+d) % 23 = p ;(n+d) % 28 = e ;(n+d) % 33 = i ; 求最小的n. 思路: 这道题就是 ...

  5. hdu 1370 || poj 1006 简单的中国剩余定理或者暴力

    Biorhythms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Probl ...

  6. POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...

  7. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  8. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  9. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

随机推荐

  1. codeforces@281 B

    shui #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  2. ARM嵌入式复习

    第一章 1.嵌入式系统 “以应用为中心,以计算机技术为基础,软硬件可裁剪,适用于应用系统对功能,可靠性,成本,体积,功耗要求严格的专用计算机系统” 根据应用的要求,沿着“体积小,低功耗,高可靠”方向发 ...

  3. Win32++:可替代MFC的Windows桌面应用开发框架

    写在前面 有过Win32编程经验的朋友都知道,使用Windows提供的API开发桌面应用是相当繁琐的,创建一个功能简单能接收并处理消息的窗口至少也得几百行代码.创建一个可视化的窗口一般要以下几个步骤: ...

  4. form表单加密前台js后台java

    1.前台javascript 1.在提交的js中这样写 <!-- 加密解密 --> document.form1.username.value=encode64(document.form ...

  5. 提高realm存储速率

    我的数据量大约有2.5M,但是完全存储到数据库差不多用了11秒,有没有比较好的方法提高存储效率 提高realm存储速率 >> android这个答案描述的挺清楚的:http://www.g ...

  6. php函数: call_user_func()和call_user_func_array() 使用详解

    call_user_func 该函数允许直接调用自己写的函数,可以直接传入一些参数. 使用方法1:给自己写的函数传入参数,一个特别的调用函数的方法. <?php funciotn test1($ ...

  7. UI Framework-1: views

    views Overview and background Windows provides very primitive tools for building user interfaces. Th ...

  8. php八大设计模式之策略模式

    策略模式提供一个虚拟的整体,根据不同的要求(参数)提供不同的"零件"(调用不同的"零件"实现不同的结果). <?php /** * 策略模式 * 跟工厂模 ...

  9. bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  10. Java web application——Listener

    应用程序事件提供ServletContext和HttpSession以及ServletRequest对象状态更改的通知,用户编写响应状态更改的事件监听器类,并配置和部署他们.Servlet容器会调用事 ...