POJ 1006:biorhythms Chinese remainder theorem

Source: Internet
Author: User

Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 121194 Accepted: 38157

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.

Test instructions is in the life of three kinds of biological rhythm, physical, emotional and intellectual, each mood has a corresponding cycle, to the point of this cycle this person becomes special cock.

Now give you these three cycles of 23 28 33. It's fixed.

And then give you the top moment of the last three cycles is P e i, asking how long it will take from D time to n days, and three cycles to reach the peak together.

The column formula is n+d = P+23*N1 = E+28*n2 = I+33*n3

N1,n2,n3 is an integer we don't have to worry about or ask for. The requirement is n.

Another transformation is to find a number of the smallest s, this number s divided by 23 p, divided by 28 E, divided by 33 I. Of course the last request of the n just s-d just fine.

It's a gesture again. Chinese remainder theorem.

First, meet the minimum number of three conditions divided by 23 to 1, divisible by 28, divisible by 33, and found to be 5544.

Then find the minimum number of three conditions divided by 28 to 1, divisible by 23, divisible by 33, is 14421.

Then find the minimum number of three conditions divided by 33 to 1, divisible by 23, divisible by 28, is 1288.

Then the 5544*p+14421*e+1288*i must be the next peak of the three cycle, but not necessarily the minimum value.


Here's why it's done:

1. A number a divided by 23 1,b divided by 23, then (A+B) divided by 23 is still more than 1. That is, a number plus the number of divisible X, divided by the remainder of X is constant.

So for 23来, the remainder of the a+b+c is still 1, because B and C are divisible by 23.

For 28来, the a+b+c remainder is 1, because A and C are divisible by 28.

For 33来, the a+b+c remainder is 1, because A and B are divisible by 33.

2.A divided by more than 23 1,a*c divided by 23 c*1, that is, more than C.

So the conclusion is that 5544*p is divided by 23 and the remainder is divided by 28, divided by 33.

14421*e divided by 28 E, divided by 23, divisible by 33, divided by

1288*i divided by 33 I, divided by 23, divisible by 28.


So these three add to meet the conditions, but not necessarily the minimum value, how to find the minimum value%LCM (23,28,29) can be.

Code:

#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string > #include <cstring> #pragma warning (disable:4996) using namespace Std;int main () {int A,b,c,d,j=1;while (CIN >>A>>B>>C>>D) {if (a+b+c+d==-4) break;cout<< "Case" <<j;j++;cout<< ": the Next triple peak occurs in "; int temp= (5544*a+14421*b+1288*c-d+21252)%21252;if (temp==0) {cout<<21252;} elsecout<<temp;cout<< "Days." <<endl;} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1006: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.