Solution for 1006 China remainder theorem

Source: Internet
Author: User

first, the requirements http://poj.org/problem?id=1006 biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 124113 Accepted: 39154

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 on 10789 days.  

probably means that the physical, emotional and intellectual states of man are cycles of 23 days, 28 days and 33 days respectively, One day in each loop is the peak day of the indicator. Enter P,e,i,d. Respectively, the P-day strength is the peak, the e-day sentiment is the peak, the first day of intelligence is the peak, and the current is D day. The peak day of physical, emotional, and mental intelligence is the same day that you ask for it.
This topic can be solved with brute force, But using the Chinese remainder theorem to solve this problem is undoubtedly a more elegant approach.

Ii. Chinese remainder theorem

  The mathematics work of the Chinese Northern and Southern Dynasties (A.D. 5th century), "grandson of the book", the 26th issue, called "Things do not know the number" problem, the original text is as follows:
There is no known number, 33 of the remaining two, 55 of the remaining three, 77 of the remaining two. Asking for geometry?
That is, an integer divided by three more than two, divided by five more than three, divided by seven more than two, the integer is asked.

As I understand it, the divisor is three prime numbers 3, 5, 7, so the integers that satisfy this condition must be in the 3*5*7=105 cycle. We just need to ask for a number that satisfies the condition, dividing it by 105 the remainder is the smallest solution that satisfies the condition.

Let's first find the first condition: divide by three by two. Here we are looking for a common multiple of 5 and 7 in order to keep the divisor 5 and 7 from causing trouble:

Least common multiple of 5 and 7:35. 35%3=2 Well, we found the first number a=35

Again, find the second number:

Least common multiple of 3 and 7:21. 21%5=1 we want to divide five to three, so multiply the result by 3, (21*3)%5=3 the second number b=63

Then find the third number:

Least common multiple of 3 and 5:15. 15%7=1--------> (15*2)%7=2 c=30

We see A: In addition to 3 + 2 in addition to more than 5 0 in addition to more than 7 0

B: In addition to more than 3 0 except 5 + 3 except 7 0

C: In addition to more than 3 0 except 5 + 0 except 7 2 We add up these three numbers

σ= D: In addition to more than 3 2 in addition to more than 5 3 in addition to more than 7 2 =128 are simultaneously meet the number of these three conditions!

Obviously D is not the minimum number to satisfy this condition, minus 105 to get 28 is the solution to this problem.

Third, the application

Back to 1006, take the third line of input: 5 20 34 325 For example, x in addition to 23 5, in addition to 28 more than 20, except 33 34 (that is, 1). Can get

a:28*33=924 924 In addition to 23 more than 4, and we want to be more than 5 of the number, how to do? We multiply 924 from 1 to 23, and we can certainly find the remainder is 5 result: 6468.

Similarly. b=12144,c=1288

x=a+b+c=19990, minus 325 to get 19575 is what you ask for.

Four, the code

I wrote it in Notepad when I was at work.

1#include <stdio.h>2 3 intP,e,i,p1,e1,i1,t1,t2,t3,t,sum,result,j,count;4p= at, e= -, i= -, count=1;5 6 voidCalintP1,intE1,intI1,intt)7 {8t1=e*i;9      for(j=0; j<p;j++)Ten     { One         if(t1%p==p1%p) A         { -              Break; -         } thet1+=e*i; -     } -      -t2=p*i; +      for(j=0; j<e;j++) -     { +         if(t2%e==e1%e) A         { at              Break;  -         } -t2+=p*i; -     } -      -t3=p*e; in      for(j=0; j<i;j++) -     { to         if(t3%i==i1%i) +         { -              Break; the         } *t3+=p*e; $     } Panax Notoginseng  -sum=t1+t2+T3; theresult=sum; +      A     if(sum<=t) the     { +printf"Case %d:the Next triple peak occurs in%d days.\n", count,sum-t); -     } $     Else $     { -          while(sum>t) -         { thesum-=21252; -         }Wuyiprintf"Case %d:the Next triple peak occurs in%d days.\n", count++,sum-t+21252); the     } - } Wu intMain () - { About      while(1) $     { -scanf"%d %d%d%d",&p1,&e1,&i1,&t); -         if(p1==-1) -         { A              Break; +         } the         Else -         { $ cal (p1,e1,i1,t); the         } the     } the  the      -     return 0; in}



Solution for 1006 China 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.