POJ-1006: biorhythms 详解2: 叠加取余

> 分析

>> 因为days可能小于p, e, i, 所以首先让p,e,i等于各自第一次循环的起点

>> 分三步进行叠加, 根据题意days从 d+1 开始

1. 计算第一个 days % 23 == p 的点(这一步可以一次计算出来)

2. 从上一步的结果开始,找到第一个 days % 28 == e 的点, 每次增加 23

3. 从上一步的结果开始,找到第一个 days % 33 == i 的点, 每次增加 23 * 28

> 注意

>> 计算出days一定要大于d, 因此循环可以直接从 d+1开始

>> 如果使用了循环结束判断,则注意days <= 21252 + 365

> 附代码

 1 /* ------------------------------
 2  * 叠加取余
 3  * ----------------------------*/
 4 #include "stdio.h"
 5
 6 #define CYCLE_P 23
 7 #define CYCLE_E 28
 8 #define CYCLE_I 33
 9
10 int main(void)
11 {
12     int p = 0, e = 0, i = 0, d = 0 ;
13     int count = 0 ;
14     int days = 0 ;
15
16     while(1)
17     {
18         scanf("%d %d %d %d", &p, &e, &i, &d) ;
19         if(-1 == d)
20             break ;
21
22         p = p % CYCLE_P ;
23         e = e % CYCLE_E ;
24         i = i % CYCLE_I ;
25
26         days = d + 1;
27
28         if(days <= p)
29             days = p ;
30         else
31             days += CYCLE_P - (days - p) % CYCLE_P ;
32
33         while(days % CYCLE_E != e)
34             days += CYCLE_P ;
35
36         while(days % CYCLE_I != i)
37             days += CYCLE_P * CYCLE_E ;
38
39         printf("Case %d: the next triple peak occurs in %d days.\r\n",
40             ++count, days - d) ;
41     }
42
43     return 0 ;
44 }
时间: 2024-10-31 05:11:56

POJ-1006: biorhythms 详解2: 叠加取余的相关文章

PKU POJ 1006 Biorhythms (中国剩余定理)

中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   Mi*xi + mi*yi = 1 令ei = Mi*xi ,则有: 则e0a0 + e1a1 + e2a2+ - +en-1*an-1是方程一个解 因为n%3=2,n%5=3,n%7=2且3,5,7互质       使5×7被3除余1,用35×2=70:        使3×7被5除余1,用21×1

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

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, respec

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

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 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,

poj 1942(详解)

Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21439   Accepted: 5259 Description Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastere

POJ 1006 Biorhythms (数论-中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111285   Accepted: 34638 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,

poj 1006 Biorhythms (中国剩余定理学习)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 113517   Accepted: 35597 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,

jQuery$.each循环遍历详解,各种取值对比,$.each遍历数组、对象、Dom元素、二维数组、双层循坏、类json数据等等

jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合中. .children() 获得匹配元素集合中每个元素的所有子元素. .closest() 从元素本身开始,逐级向上级元素匹配,并返回最先匹配的祖先元素. .contents() 获得匹配元素集合中每个元素的子元素,包括文本和注释节点. .each() 对 jQuery 对象进行迭代,为每个匹配元素执行函数. .end(

poj 1006 Biorhythms

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 117289   Accepted: 36793 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,

中国剩余定理 POJ 1006 Biorhythms

题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /************************************************ * Author :Running_Time * Created Time :2015/9/15 星期二 16:53:01 * File Name :POJ_1006.cpp **********************