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 ...
随机推荐
- Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字
本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...
- 使用x manager 连接Linux桌面
/usr/bin/xterm -ls -display $DISPLAY 需要安装xterm 服务
- python打包成exe
目前有三种方法可以实现python打包成exe,分别为 py2exe Pyinstaller cx_Freeze 其中没有一个是完美的 1.py2exe的话不支持egg类型的python库 2.Pyi ...
- python之单例设计模式
设计模式之单例模式 单例设计模式是怎么来的?在面向对象的程序设计中,当业务并发量非常大时,那么就会出现重复创建相同的对象,每创建一个对象就会开辟一块内存空间,而这些对象其实是一模一样的,那么有没有办法 ...
- python读取word表格内容(1)
1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格
- COB(Chip On Board) 工艺技术
COX(Chip On X) •X 基板: PCB (Printed circuit board) FPC (Flexible Printed Circuit) Glass •导线焊接 球形焊接 ...
- 完美解决CTRL+空格不能切换中/英文输入法的问题
首先任务栏上的输入法图标上点右键选择设置. 然后选择键设置,双击第一个“在不同的输入语言之间切换”先勾选“切换输入语言”下面选择左手ALT.取消右边“切换键盘布局”前的勾. 然后进入“中文(简体)输入 ...
- Java和Android开发IDE---IntelliJ IDEA使用技巧(转)
以前一直使用的是Eclipse,听别人介绍说IDEA非常不错,也为了以后转Android studio铺垫下.就开始尝试用idea来开发. 这篇文章主要学习了idea的使用技巧. IDEA 全称 In ...
- 字符串的MD5的32位加密和16位加密
import java.security.MessageDigest; import java.util.Locale; public class MD5Util { public static St ...
- vb6.0 倒计时
Dim t Dim start As Boolean Private Sub Command1_Click() If start = False Then t = Val(Text1) * 3600 ...