POJ——T 1006 Biorhythms

http://poj.org/problem?id=1006

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 138219   Accepted: 44274

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.

CRT+细节处理
 1 #include <algorithm>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 int a[4],str,m[3],p[4],cnt,tot,ans;
 7
 8 void exgcd(int a,int b,int &x,int &y)
 9 {
10     if(!b) { x=1; y=0; return ; }
11     exgcd(b,a%b,x,y);
12     int tmp=x; x=y;
13     y=tmp-a/b*y;
14 }
15 int CRT()
16 {
17     int ret=0;
18     for(int i=1;i<=3;i++)
19     {
20         int t=tot/p[i],x,y;
21         exgcd(t,p[i],x,y);
22         ret=(ret+t*x*a[i])%tot;
23     }
24     return ret>=0?ret:(ret+tot);
25 }
26
27 int main()
28 {
29     p[1]=23; p[2]=28; p[3]=33;
30     tot=p[1]*p[2]*p[3];
31     for(;scanf("%d%d%d%d",&a[1],&a[2],&a[3],&str);)
32     {
33         if(a[1]==-1&&a[2]==-1&&a[3]==-1&&str==-1) break;
34         ans=CRT()-str;  cnt++;
35         for(;ans<=0;) ans+=tot;
36         printf("Case %d: the next triple peak occurs in %d days.\n",cnt,ans);
37     }
38     return 0;
39 }
时间: 2024-12-12 17:22:42

POJ——T 1006 Biorhythms的相关文章

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,

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

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 (数论-中国剩余定理)

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

题目传送门 题意: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 **********************

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,

1006 biorhythms

思路:与求最大公约数基本方法一致.虽然起点不同但终点仍旧一致,起点也可以求出相减为0的情况. 注意:不需要用暴力枚举的方法.我们在进行枚举时筛选.以最小的23进行倍数增长一次试验. 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 int main(){ 6 int p,i,e,d,count=1; 7 while(true){ 8 cin>>

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso