POJ 1006 biorhythms (number theory-Chinese remainder theorem)

Source: Internet
Author: User

Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 111285 Accepted: 34638

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.

Source

East Central North America 1999

Main topic:

People from birth have physical, emotional and intellectual three physiological cycles, respectively, 23, 28 and 33 days. One day in a cycle is the peak, on this day, the person in the corresponding aspect (physical, emotional or intellectual) performance best. Typically, the peaks of these three cycles will not be the same day. Now give three dates, respectively, corresponding to the physical, emotional, intellectual peak of the date. Then give a starting date, asking from the beginning of the day, to figure out the minimum number of days after the three peaks at the same time.


Problem Solving Ideas:

First of all, the problem: the desire to start from one day, calculate how many days after the three peaks at the same time, set the required value for ans

Suppose we can find one day, three peaks at the same time, we remember this day as X, and then remember the strength, emotion, intelligence peak date is A,b,c, the starting date is D.

Then must: satisfies the condition x=a+23*t1=b+28*t2=c+33*t2.

Then look at the relationship between ans and x: ans = (x-d)% (23*28*33), so we can find X.

For X to use the Chinese remainder theorem


"Go" from: Click to open the link

Introduction to the Chinese remainder theorem

In the grandson of the book, there is a question: "This is not known, 33 of the remaining two (divided by 3 2), 55 of the remaining three (divided by 5 3), 77 of the remaining two (divided by 7 2), asked the geometry? "This problem is called" grandson Problem ", the general solution of the problem is called the" Chinese remainder theorem "internationally. The detailed solution is divided into three steps:

    1. Find out three numbers: from the common multiple of 3 and 5 to find the smallest number 1 of 7 remainder 15, from the common multiple of 3 and 7 to find the smallest 5 of 1 of the remainder 21, and finally from the common multiple of 5 and 7 to find the smallest 3 of the 1 + 70.
    2. Multiply by 15 by 2 (2 is the remainder of the result divided by 7), 21 times 3 (3 is the remainder of the result divided by 5), in the same vein, 70 times 2 (2 is the remainder of the result divided by 3), then the three products are added (15*2+21*3+70*2) and 233.
    3. The remainder 23 is obtained by dividing 233 by 3,5,7 Three least common multiple 105, which is 233%105=23. The remainder of 23 is the decimal number that meets the criteria.

analysis of the remainder theorem in China

We split the "grandson problem" into a few simple little questions, starting from scratch and trying to measure how the ancients deduced the solution.

First of all, if N1 is satisfied by dividing by more than 3 2 of a number, such as 2,5,8 and so on, that is to meet 3*k+2 (k>=0) a random number. In the same way, if N2 is satisfied by dividing by more than 5 3 of a number, N3 is satisfied by dividing by 7 by a number of 2.

With the previous if, we first from N1 this point of view, known N1 meet divided by 3 more than 2, can make n1+n2 and still meet divided by 3 more than 2? So that the n1+n2+n3 and still meet divided by 3 more than 2?

This involves one of the most basic mathematical theorems, false with A%b=c, then there is (A+KB)%b=c (k is nonzero integer), in other words, assuming a division operation of the remainder is C, then the divisor of dividend and k multiplied (or subtracted) and (difference) and divide the divisor, the remainder is unchanged. This is a very good proof.

On the basis of this theorem, assuming that the N2 is a multiple of 3, N1+N2 is still satisfied by dividing by 3 + 2. Similarly, assuming that N3 is also a multiple of 3, then n1+n2+n3 and satisfies are divided by 3 + 2. This is from the perspective of N1, and then from the perspective of n2,n3, we can deduce the following three points:

    1. To make the n1+n2+n3 and meet divided by more than 3 2,n2 and N3 must be a multiple of 3.
    2. To make the n1+n2+n3 and meet divided by more than 5 3,n1 and N3 must be a multiple of 5.
    3. To make the n1+n2+n3 and meet divided by more than 7 2,n1 and N2 must be a multiple of 7.

Therefore, in order to make the n1+n2+n3 and as a "grandson problem" a finally solution, need to meet:

    1. N1 divided by 3 and 2, and is common multiple of 5 and 7.
    2. N2 divided by 5 and 3, and is common multiple of 3 and 7.
    3. N3 divided by 7 and 2, and is common multiple of 3 and 5.

Therefore, the essence of Sun Tzu's solution is to find a common multiple of 5 and 7 divided by 3 2 N1, from 3 and 7 of common multiple to find a number divided by 5 3 N2, from the common multiple of 3 and 5 to find a number divided by 7 2 N3, and then add three numbers to get the solution. In the search for n1,n2,n3 and a small trick, take N1 as an example, not from the 5 and 7 of the common multiple directly to find a number divided by 3 2, but first to find a divided by 3 more than 1 of the number, and then multiplied by 2.

Here again there is a mathematical formula, assuming a%b=c, then (a*k)%B=A%B+A%B+...+A%B=C+C+...+C=KC (k>0), that is, assuming that the remainder of a division is C, then the remainder of the dividend's K-fold divide with the divisor is KC. has been proven in the expanded formula.

Finally, we have to be clear that N1+N2+N3 is only a solution to the problem, not the smallest solution. How to get the minimum solution? We just need to minimize the 3,5,7 of common multiple 105 from it. The truth is that the theorem "hypothesis A%b=c, there is (A-KB)%b=c". So (N1+N2+N3)%105 is finally the smallest solution.


Problem Solving Code:

#include <iostream> #include <cstdio>using namespace Std;int a,b,c,d;int n1,n2,n3;void get () {//get n1,n2,n3 for    (int i=1;; i++) {        if (28*33*i%23==1) {            n1=28*33*i;            break;        }    }    for (int i=1;; i++) {        if (23*33*i%28==1) {            n2=23*33*i;            break;        }    } for (int i=1;; i++) {        if (23*28*i%33==1) {            n3=23*28*i;            Break;}}}    int main () {    get ();    int casen=0;    while (scanf ("%d%d%d%d", &a,&b,&c,&d)!=eof) {        if (a==-1 && b==-1 && c==-1 & & D==-1) break;        int ans= (n1*a+n2*b+n3*c-d)% (23*28*33);        ans=ans<=0?ans+23*28*33:ans;        printf ("Case%d:the next triple peak occurs in%d days.\n", ++casen,ans);    }    return 0;}




POJ 1006 biorhythms (number theory-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.