poj1006 China remainder theorem && analysis of Chinese remainder theorem

Source: Internet
Author: User

The idea of POJ 1006 is not very difficult and can be transformed into mathematical formula:

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 meaning of the title is, n% 3 = 2, n 5 = 3, n 7 = 2; Q What 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 divisible by 3 and 7, but%5 equals 3
span>

(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; * * 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 scope only

The following is attached to the poj1006 basic Chinese remainder theorem classic examples

Physiological cycle

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 122201 Accepted: 38531

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).

The following format is used:
Case 1:the Next triple peak occurs in 1234 days.

Note: Even if the result is 1 days, also use the plural form "day".

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

Translator

Beijing University Program Design Internship, Xie Di
/*China's remainder theorem: from the grandson of the Bible*/#include<stdio.h>#defineMAX 21252intMain () {intP, E, I, D, n, Count =0;  while(SCANF ("%d%d%d%d", &p, &e, &i, &d)! =EOF) {Count++; if(p = =-1&& E = =-1&& i = =-1&& D = =-1)        {             Break; } N= (5544* p +14421* E +1288* i-d)%MAX; if(N <=0)//Scope Limits{n+=21252; } printf ("Case %d:the Next triple peak occurs in%d days.\n", Count, N); }    return 0;}

poj1006 China remainder theorem && analysis of 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.