POJ-1006 Biorhythms
【题目描述】
三个周期时间分别为:23,28和33。分别给定三个周期的某一天(不一定是第一天),和开始计算的日期,输出下一个triple peak。
【思路分析】
如果不了解中国剩余定理,可以通过模拟的方式:从开始日期起,寻找第一次遇到高峰的项目,记录;之后寻找该项目的下一个高峰,测试是否另外两个项目也是高峰。
若用中国剩余定理求解,则求:(n+d)%23=p; (n+d)%28=e; (n+d)%33=i,gcd(23, 28, 33)=1。
记p, e, i分别为a1, a2, a3. 并设M = m1*m2*m3 = 23*28*33 = 21252. Mi = M / mi. 故M1 = 28*33 = 924; M2 = 23*33 = 759; M3 = 23*28 = 644.
则解x = a1t1M1 + a2t2M2 + a3t3M3, 其中tiMi=1(mod mi). 下面将求解ti:
ti 实际上为 Mi 模 mi 的逆。可采用辗转相除法得出,见百度文库:
http://wenku.baidu.com/view/ada3397f2f60ddccdb38a04b.html
http://wenku.baidu.com/view/f0894659be23482fb4da4c51.html
【小结】
复习一下中国剩余定理
【附:完整代码】
#include <iostream>
using namespace std; int main()
{
int p, e, i, d, caseno = 1;
const int CIRCLE[3] = {23,28,33};
while (true)
{
cin>>p>>e>>i>>d;
if (p == -1 && e == -1 && i == -1 && d == -1)
break; int testday = d + 1;
int largestIndex;
int largestDay; // 记录第一次遇到高峰的日期testday,和这个高峰是属于哪个项目
while (true)
{
if ((testday-p) % CIRCLE[0] == 0)
{
largestIndex = 0;
largestDay = p;
break;
}
else if ((testday-e) % CIRCLE[1] == 0)
{
largestIndex = 1;
largestDay = e;
break;
}
else if ((testday-i) % CIRCLE[2] == 0)
{
largestIndex = 2;
largestDay = i;
break;
}
else
{
testday++;
}
} // 寻找三个高峰的日期
while (true)
{
if ((largestIndex == 0 || (testday-p) % CIRCLE[0] == 0) &&
(largestIndex == 1 || (testday-e) % CIRCLE[1] == 0) &&
(largestIndex == 2 || (testday-i) % CIRCLE[2] == 0))
{
int resultday = testday - d;
cout<<"Case "<<caseno++<<": the next triple peak occurs in "<< resultday <<" days."<<endl;
break;
} testday += CIRCLE[largestIndex];
} } return 0;
}
POJ-1006 Biorhythms的更多相关文章
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- [POJ 1006] Biorhythms C++解题
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 107569 Accepted: 33365 ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- [POJ] #1006# Biorhythms : 最小公倍数/同余问题
一. 题目 Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127263 Accepted: 403 ...
- POJ - 1006 Biorhythms 周期相遇 两个思路程序
Description Some people believe that there are three cycles in a person's life that start the day he ...
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
随机推荐
- SQL类型转换以及自动在前面补0满足10位工号标示法
1,自动在前面补0满足10位工号标示法 SELECT rtrim(ltrim(right(cast('00000000'+rtrim(CAST(数值 as int)) as varchar(20)), ...
- Mvc--Html.ActionLink()用法
},new{ target="_blank"})会生成 <a href="Products/Detail/1" target="_blank&q ...
- javascript封装自定义滚动条方法,可自定义四个边框滚动条
还是根据我的个人习惯封装了一个方法 setScroll({ box :父盒子DOM对象, content : 内容盒子DOM对象, scrollall : 滚动条大盒子DOM对象, scroll : ...
- Svn入门
1.建立svn仓库 命令svnadmin create 仓库名称,如:进入命令行窗口,切换到Svn安装目录下,输入如下命令:svnadmin create F:\software\repos ...
- js基础参数获取
1 获取浏览器中url中的参数,会自动把问号"?"去掉 function getParamsFromHref() { //调试用 var wyl_ = window.locatio ...
- ubuntu中查找软件的安装位置
ubuntu中的软件可通过图形界面的软件中心安装,也可以通过命令行apt-get install安装.但是安装后的软件在哪个位置呢?这点跟windows环境下安装软件的路径选择不一样.ubuntu中可 ...
- PCB流程-外型加工
- WS_CLIPCHILDREN与WS_CLIPSIBLINGS 收藏
英文单词解释clip:夹子.子弹夹.回形针:夹住,修剪sibling:同胞兄弟或姐妹overlapped:重叠 这两个Window Stype的特性与异同素来不太清楚,今日作一笔记:MSDN的解释为: ...
- java.lang.NoClassDefFoundError: javax/servlet/AsyncContext
报错:java.lang.NoClassDefFoundError: javax/servlet/AsyncContext 我认为你需要在Servlet API,而不是2.5.AsyncContext ...
- Oracle闪回操作
Oracle闪回操作 1. 记录当前时间或SCN 在数据库变动前记录时间或SCN SQL> select to_char(sysdate,'YYYY-MM-DD HH24:mi:ss') fr ...