Biorhythms

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 127443   Accepted: 40379

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

思路:中国剩余定理+扩展欧几里德求逆元;

也可以暴力,数据水

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<queue>
 7 #include<stack>
 8 #include<math.h>
 9 using namespace std;
10 typedef long long LL;
11 int ans[4];
12 int bns[4];
13 pair<LL,LL>P(LL n,LL m);
14 int main(void)
15 {
16     int i,j,k;
17     int x1,x2,x3,x4;
18     int an=0;
19     while(cin>>x1>>x2>>x3>>x4,x1!=-1&&x2!=-1&&x3!=-1&&x4!=-1)
20     {
21         an++;
22         ans[0]=x1%23;
23         ans[1]=x2%28;
24         ans[2]=x3%33;
25         bns[0]=23;
26         bns[1]=28;
27         bns[2]=33;
28         LL sum=1;
29         for(i=0; i<3; i++)
30         {
31             sum*=bns[i];
32         }
33         LL  ak=0;
34         for(i=0; i<3; i++)
35         {
36             LL kk=sum/bns[i];
37            pair<LL,LL>v=P(kk,(LL)bns[i]);
38             LL cp=v.first;cp=(cp%sum+sum)%sum;
39             kk*=ans[i];
40             kk*=cp;
41             ak+=kk;
42             ak%=sum;
43         }
44         while(ak<=x4)
45         {
46             ak+=sum;
47         }
48         ak-=x4;
49         if(ak>sum)
50         {
51             ak=sum;
52         }
53         printf("Case %d: ",an);
54         printf("the next triple peak occurs in %lld days.\n",ak);
55
56     }
57     return 0;
58 }
59 pair<LL,LL>P(LL n,LL m)
60 {
61     if(m==0)
62     {
63         pair<LL,LL>C=make_pair(1,0);
64         return C;
65     }
66     else
67     {
68         pair<LL,LL>M=P(m,n%m);
69         LL x=M.second;
70         LL y=M.first;
71         y-=(n/m)*x;
72         M.first=x;
73         M.second=y;
74         return M;
75     }
76 }
时间: 2024-11-08 23:32:59

Biorhythms的相关文章

Biorhythms(中国剩余定理)

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

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status 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

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

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,

Biorhythms POJ - 1006

Biorhythms POJ - 1006 题意: 求解一元线性同余方程组. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 const int maxn=1010; 5 int b[maxn],m[maxn]; 6 int n; 7 void exgcd(int a,int b,int &d,int &x,int &y){ 8 if(!b){d=a;x=1;y=0;} 9

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

uva 756 - Biorhythms(中国剩余定理)

题目链接:uva 756 - Biorhythms 题目大意:三个周期,23,28,33,输入为分别为在新一年中(三个周期均从0开始),出现周期中峰值的一天,以及当前的日子,问说需要经过多少天,才能使得三个峰值的在同一天. 解题思路:裸的中国剩余定理. #include <cstdio> #include <cstring> typedef long long ll; const int maxn = 5; const ll m[maxn] = {23,28,33}; ll M,

poj1006 Biorhythms

Biorhythms POJ - 1006 题意:人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天.一个周期内有一天为峰值,在这一天,人在对应的方面(体力,情感或智力)表现最好.通常这三个周期的峰值不会是同一天.现在给出三个日期,分别对应于体力,情感,智力出现峰值的日期.然后再给出一个起始日期,要求从这一天开始,算出最少再过多少天后三个峰值同时出现. /* x=p(mod 23) x=e(mod 28) x=i(mod 33) 题目就是要求上面同余方程的解,用中国剩余定理求解

CSUOJ 1858 Biorhythms 中国剩余定理

1858: Biorhythms Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: 69     Solved: 37 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 cycl