Biorhythms (Chinese remainder theorem)

Source: Internet
Author: User

Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 127339 Accepted: 40342

Description

Some people believe that there be three cycles in a person's life that's start the day he or she's born. These three cycles is the physical, emotional, and intellectual cycles, and they have periods of lengths, D, and Ays, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes would be sharper and concentration would be easier.Since The three cycles has 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'll be given the number of days from the beginning of the current year at which one of its peaks (no T necessarily the first) occurs. You'll also be given a date expressed as the number of days from the beginning of the current year. You task was 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 ten and the next triple peak occurs on day, the answer was 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'll be given a number of cases. The input for each case consists of a line of four integers p, E, I, and D. The values P, E, and I are is the number of days from the beginning of the which the physical, emotional, and Intellectual cycles peak, respectively. The value D is the given date and could be smaller than any of P, E, or I. All values is non-negative and at most 365, and your may assume that a triple peak would occur within 21252 days of the GIV En 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 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.
Exercises

Description

Humans are born with three cycles of physical, emotional and intellectual cycles, with periods of 23 days, 28 days and 33 days. One day in each cycle is a peak. At the peak of the day, people will do well in the corresponding aspects. For example, the peak of the intellectual cycle, people will be quick thinking, energy is easy to highly concentrated. Because the circumference of the three cycles is different, the peak of three cycles usually does not fall on the same day. For everyone, we want to know when three peaks fall on the same day. For each cycle, we will give the number of days from the first day of the current year to the peak (not necessarily the time of the first peak). Your task is to give a number of days from the first day of the year, and the output starts from a given time (excluding a given time) the next three peaks fall on the same date (the number of days from a given time). For example: Given a time of 10, the next occurrence of three peaks on the same day is 12, then output 2 (note that this is not 3).

Input

Enter four integers: p, E, I, and D. P, E, I, respectively, indicate the time at which the physical, emotional and intellectual peaks appear (the time is calculated from the first day of the year). D is the given time, which may be less than P, E, or I. All given times are non-negative and less than 365, and the time required is less than 21252.
When p = e = i = d =-1, the input data ends.

Output

From a given time, the next three peaks of the same day (the number of days from a given time).
Use the following format: Case 1:the Next triple peak occurs in 1234 days.
Note: Even if the result is 1 days, also use the plural form "day". Exercises

Now num is the number of days from the next same day to the beginning

P,e,i,d title in the set!

Then you can get three formulas: (num + d)% = = p, (num + d)% = = e; (num + d)% = = i;

P,e,i,d is our input, then we need to find Num can, for convenience, we will num+d temporarily as a whole! make x = num + D;

namely: x% = = p; x% = = e; x% = = i; x

What to do? This involves the so-called " Chinese remainder theorem " (The concept of Google, very easy)

"The grandson of the book" in the "things do not know the number" problem: "There is no knowledge of its number, 33 of the number of two, 55 number of three, 77 number of two, asked the geometry of the matter?" "The answer is" 23 ".

--------This is the legendary "Chinese remainder theorem". In fact, the title means, n% 3 = 2, n% 5 = 3, n 7 = 2; Q How much is n?

So how did he fix it?

See below:

The topics involved 3, 5, 73 coprime number,

Order: 5 * 7 * A% 3 = 1; --------------> A = 2; i.e. 5 * 7 * 2 = 70;

3 * 7 * b% 5 = 1; --------------> b = 1; i.e. 3 * 7 * 1 = 21;

3 * 5 * c% 7 = 1; --------------> c = 1; i.e. 3 * 5 * 1 = 15;

Why the remainder is 1: In order to require the remainder of 2, as long as the sum of 2 can be, the remainder of 3 is required, as long as the 3 can be!

(Because the topic wants n% 3 =2, n% 5 =3, n% 7 =2;  )

So: to make n% 3 = 2, then (5 * 7 * 2) *2 % 3 = 2; (Because 5 * 7 * 2% 3 = 1 )

Similarly: to make n% 5 = 3, then (3 * 7 * 1) *3 % 5 = 3; (Because 3 * 7 * 1% 5 = 1)

Similarly: to make n% 7 = 2, then (3 * 5 * 1) * 2 % 7 = 2; (Because 3 * 5 * 1% 7 = 1)

So now will (5 * 7 * 2) * 2 and (3 * 7 * 1) * 3 and (3 * 5 * 1) * 2 add up what will happen? We know

(5 * 7 * 2) * 2 can be divisible by 5 and 7, but%3 equals 2

(3 * 7 * 1) * 3 can be divisible by 3 and 7, but%5 equals 3

(3 * 5 * 1) * 2 can be divisible by 3 and 5, but%7 equals 2

So even after the addition,%3, 5, 7 of the situation is still the same!

Then get a number we need temporarily (5 * 7 * 2) * 2 + (3 * 7 * 1) * 3 + (3 * 5 * 1) * 2 = 233

But not the smallest! All we need is 233% (3 * 5 * 7) = = 23 Solution!

/************************************************************************************************************** ****************************************/

The above is the algorithm analysis, seemingly speaking is not very clear, hey, we forgive the ~

Now look at this topic: x% = = p; x% = = e; x% = = i; x

According to the above algorithm:

Make * A% 23 = 1, get a = 6; 33 * 28 * 6 = 5544;

Make a total of * b% 28 = 1, b = 19;23 * 33 * 19 = 14421;
Make * * C% 33 = 1, c = 2; 23 * 28 * 2 = 1288.

So x = 5544 * p + 14421 * e + 1288 * I

Then x-d is the difference between the time and days!

Because there is scope limit, then (x-d)%= 21252, and if at this time <=0, then (x-d) + = 21252, the above is only to guarantee in the range only ~

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;BOOLjsintAintBintCintd) {    if(A = = b && b = = c && c = d && A = =-1)        return false; return true;}intMain () {intP, E, I, D, Kase =0;  while(~SCANF ("%d%d%d%d", &p, &e, &i, &d), JS (P, E, I, D)) {intA, B, C;  for(A =1; ; a++){            if( -* -*a% at==1) {a= -* -*A;  Break; }        }         for(b =1; ; b++){            if( -* at*b% -==1) {b= -* at*b;  Break; }        }         for(c =1; ; C++){            if( at* -*c% -==1) {C= at* -*B;  Break; }        }    //printf ("%d%d%d\n", A, B, c);        intsum = a*p + b*e + c*i; intx = (sum-d)%21252; if(x <=0) x + =21252; printf ("Case %d:the Next triple peak occurs in%d days.\n", ++Kase, X); }    return 0;}

Biorhythms (Chinese remainder theorem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.