【题目描述】

三个周期时间分别为: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的更多相关文章

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

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

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

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

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

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

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

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

  5. [POJ 1006] Biorhythms C++解题

        Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 107569   Accepted: 33365 ...

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

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

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

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

  8. [POJ] #1006# Biorhythms : 最小公倍数/同余问题

    一. 题目 Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127263   Accepted: 403 ...

  9. POJ - 1006 Biorhythms 周期相遇 两个思路程序

    Description Some people believe that there are three cycles in a person's life that start the day he ...

  10. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

随机推荐

  1. mysql save or update

    原文:http://www.bitscn.com/pdb/mysql/201503/473268.html 背景   在平常的开发中,经常碰到这种更新数据的场景:先判断某一数据在库表中是否存在,存在则 ...

  2. C++_基础_C与C++的区别2

    内容: (1)C++中的函数 (2)动态内存 (3)引用 (4)类型转换 (5)C++社区对C程序员的建议 1.C++中的函数1.1 函数的重载(1)重载的概念 在同一个作用域中,函数名相同,函数的参 ...

  3. 机器学习算法实现(R&Python code)

    Machine Learning Algorithms Machine Learning Algorithms (Python and R) 明天考试,今天就来简单写写机器学习的算法 Types Su ...

  4. Use API to retrieve data from internet

    Reference: Working with APIs Many big companies and organizations provide API for us to retrieve dat ...

  5. (iOS)Storyboard/xib小技巧

    1.选择被view覆盖住的view 当你想直接在view中选择自己想要的元素时,但是又碍于一个view上叠加的元素太多很难直接选中,那么在这时,你同时按住键盘上的shift和 control键,然后在 ...

  6. OSG中的智能指针

    在OpenSceneGraph中,智能指针(Smart pointer)的概念指的是一种类的模板,它针对某一特定类型的对象(即Referenced类及其派生类)构建,提供了自己的管理模式,以避免因为用 ...

  7. JAVA并发,锁与方法

    引自:<thinking in java> synchronized void f(){/* ... */}; synchronized void g(){/* ... */}; 所有对象 ...

  8. 阿里云ECS每天一件事D9:nginx1.7整合tomcat8.0

    仅通过8080端口访问jsp显然不是一算是一个太好的方法,可以使用nginx的proxy_pass子模块,实现nginx转发jsp请求至tomcat. 典型的配置如下: server { listen ...

  9. SD卡协议规范学习

    首先,本博文遵照SD卡协议3.01版本,最旧协议版本为1.10,但是协议是向下兼容的.SD卡Physical Layer Simplified Specification Version 3.01英文 ...

  10. Netty那点事: 概述, Netty中的buffer, Channel与Pipeline

    Netty那点事(一)概述 Netty和Mina是Java世界非常知名的通讯框架.它们都出自同一个作者,Mina诞生略早,属于Apache基金会,而Netty开始在Jboss名下,后来出来自立门户ne ...