POJ1006 Biorhythms 题解

题目内容大意

题目大意:人有生理,心理以及智力的一个循环,分别经过23,28,33天一个循环到达顶峰。

输入:给你4个数,在年初的时候距离上一次的生理,心理,智力的顶峰的时间p,e,i。 以及这个年已经过了几天了d。

输出:计算下一次三项同时达到顶峰的时间。

个人想法:

这个问题一开始想通过算法解决,发现有点困难。去网上找了题解。说的是孙子定理或者是中国剩余定理,或者是中国余数定理。如果想看视频了解孙子定理,可以去西瓜视频上找李永乐老师讲《韩信点兵》的视频,李永乐老师讲孙子定理​​​​​


PART1 孙子定理 如果孙子定理视频看明白了可以直接到PART2 到题目里的应用

《孙子定理解析》如果有不对的地方请指正

引入韩信点兵

韩信点兵的时候:

3人一排 余2人,5人一排 余3人 7人一排 余2人

问有多少兵。

一、列举法

(1)

除以3余2的数有:2,5,8,11,14,17,23,26,29...

除以5余3的数有:3,8,13,18,23,28...

除以7余2的数有:2,9,16,23,30...

可以找到他们三者共同的数为23,后面可能也有

(2)由于3,5,7三个数互质,所以他们的最小公倍数为 3*5*7 = 105

所以韩信的兵可能有 23 + 105 * n  其中n为自然数

二、孙子算经的方法

明代数学家的口诀:

三人同行七十稀,

五树梅花廿一枝,

七子团圆月正半,

除百零五便得知。

解释:

除以3的余数 * 70 + 除以5的余数 * 21 + 除以7的余数 * 15得到的和最后对105取余数就可以得到最小的人数。比如韩信点兵的人数 2 * 70 + 3 * 21 + 2 * 15 = 233 。233 对 105 取余数 得到 23 和列举法的答案一致

问题的关键就在于70,21,15这三个数是怎么得来的:

三个数3,5,7

分析:

70是5和7的倍数,但是70 mod 3 = 1

21是3和7的倍数,但是21 mod 5 = 1

15是3和5的倍数,但是15 mod 7 = 1

所以我们只要找到其中两个数的倍数但是对另外一个数取余得到1就可以了。这也可以解释为什么要这些数乘以余数,因为原来余数是1,如果要得到余数是3,那么这个数直接乘3就可以了,因为这个数是另外两个数的倍数不管乘以多少,对另外的两个数而言都是可以整除的。


PART2 题目解析

按照孙子定理23,28,33

23和28的最小公倍数是644,644 mod 33 = 17 因为17*2刚好等于34

644 * 2 = 1288 mod 33 = 1 所以33的余数 要乘1288

23和33的最小公倍数是759,759 mod 28 = 3,这个数要凑的比较多,3*19 mod 28 = 1

759*19 = 14421 所以28的余数 要乘14421

28和33的最小公倍数是924,924 mod 23 = 4  4*6 = 24 刚好比23多1

924 * 6 = 5544  所以23的余数 要乘以5544。

最后把结果套进题目。因为题目是当天不算的,所以最后如果得到的数是0或者是负数,还要加上他们3个数的最小公倍数,也就是三项同时到达顶峰的循环时间。题目已经告诉我们他们三者的最小公倍数是21252

最后的公式应该就是 (p * 5544 + e * 14421 + i * 1288 + 21252 - d) mod 21252

如果结果等于0的话,就再加一个21252。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main()
{
    int p, e, i, d;
    int nums = 0;
    while(cin>>p>>e>>i>>d){
        if (p == -1 && e == -1 && i == -1 && d == -1) break;
        nums += 1;
        int temp = (p * 5544 + e * 14421 + i * 1288 + 21252 - d) % 21252;
        if (temp == 0) temp += 21252;
        printf("Case %d: the next triple peak occurs in %d days.\n", nums, temp);
    }
    return 0;
}

  • 16
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53539 Accepted: 15282 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 East Central North America 1999 生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53539 Accepted: 15282 Description 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。 Input 输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。 当p = e = i = d = -1时,输入数据结束。 Output 从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。 采用以下格式: Case 1: the next triple peak occurs in 1234 days. 注意:即使结果是1天,也使用复数形式“days”。 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 East Central North America 1999 Translator 北京大学程序设计实习2007, Xie Di

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值