POJ_1006_Biorhythms(中国剩余定理)
来源:互联网 发布:it heard 编辑:程序博客网 时间:2024/05/29 17:30
Description
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
Output
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 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 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天。每当这三个周期的峰值重合的时候,人表现为最佳状态。现在给出这三个周期在当前这一年的起始时间p、e、i,还有一个日期d,求出在d后多少天三个循环会再一次达到峰值。
分析:
首先来了解一下中国剩余定理。
有题曰:今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?
简化一下:已知 n%3=2, n%5=3, n%7=2, 求n。
这怎么求呢?
因为3、5、7互质,
令x = n%3 = 2、y = n%5 = 3、z = n%7 = 2;
使5×7×a被3除余1,有35 × 2 = 70 % 3 = 1,即a = 2;
使3×7×b被5除余1,用21 × 1 = 21 % 5 = 1,即b = 1;
使3×5×c被7除余1,用15 × 1 = 15 % 7 = 1,即c = 1。
那么n = ( 70×x+21×y+15×z )% lcm(3,5,7) = 23 为n的最小解。
所以答案就是23 + i * lcm( 3,5,7 )。
再回到这个题目,
根据题意,我们可以列出下列式子:
( x + d ) % 23 = p
( x + d ) % 28 = e
( x + d ) % 33 = i
求法是相似的,
令28 * 33 * a % 23 = 1,有5544 % 23 = 1;
令23 * 33 * b % 28 = 1,有14421 % 28 = 1;
令23 * 28 * c % 33 = 1,有1288 % 33 = 1;
因为答案要的是最小的天数,
所以n+d = (5544*p+14421*e+1288*i) % lcm(23,28,33);
即:n = (5544*p+14421*e+1288*i-d) % 21252;
防止式子算下来是负数,n = (5544*p+14421*e+1288*i-d+21252) % 21252;
注意当n=0的时候,要输出21252。
代码:
#include<iostream>#include<cmath>#include<cstring>#include<cstdio>using namespace std;int main(){ int p,e,i,d,time=0; while(1){ time++; scanf("%d%d%d%d",&p,&e,&i,&d); if(p==-1&&e==-1&&i==-1&&d==-1) break; int n; n=(5544*p+14421*e+1288*i-d+21252)%21252; if(n==0){ n=21252; } printf("Case %d: the next triple peak occurs in %d days.\n",time,n); }}
- POJ_1006_Biorhythms(中国剩余定理)
- 中国剩余定理(转)
- 中国剩余定理(韩信点兵)
- poj2891(中国剩余定理)
- 中国剩余定理(续)
- HDU1370(中国剩余定理)
- 中国剩余定理(转载)
- 中国剩余定理(笔记)
- 中国剩余定理(模板)
- 中国剩余定理(详解)
- 中国剩余定理(韩信点兵)
- 中国剩余定理(CRT)
- hdu1006Biorhythms(中国剩余定理)
- 中国剩余定理(模板)
- 中国剩余定理(转)
- 中国剩余定理(CRT)
- 中国剩余定理(韩信点兵)
- POJ1006(中国剩余定理)
- Linux中的EAGAIN含义
- windows 窗口关系
- GitHub Android 最火开源项目Top20
- 【原创】从零开始——Android 4.0 实现recovery模式,出厂设置功能
- MongoDB初体验:win7下载安装
- POJ_1006_Biorhythms(中国剩余定理)
- URAL 1353
- SQL 函数
- [小说]魔王冢(40)谈判(二)
- 多重继承
- ios 修改应用名称
- spring_hibernate.xml
- 在eclipse中复制项目的问题
- 照这样计算。。。