Poj 1006 and China Residue Theorem

Source: Internet
Author: User

The idea of poj 1006 is not very difficult. It can be transformed into a mathematical formula:

Now num is the number of days from the next day.

P, e, I, and d are set in the question!

Then we can get three sub-statements: (num + d) % 23 = p; (num + d) % 28 = e; (num + d) % 33 = I;

P, e, I, d are input, so we need to find num. For convenience, we will temporarily treat num + d as a whole! Make x = num + d;

That is, x % 23 = p; x % 28 = e; x % 33 = I; x

What should I do? This involves the so-called "China Residue Theorem" (concept google, very easy)

There is a question in Sun Tzu's computing sutra that "things do not know the number of things": "Today, things do not know the number of things. There are three, and five, and three, and seven, and two, ask things ry ?" The answer is "23 ".

-------- This is the legendary "China Residue Theorem ". In fact, the question means that n % 3 = 2, n % 5 = 3, n % 7 = 2; what is n?


So how did he solve it?

See the following:

The question involves three mass numbers: 3, 5, and 7,

Order: 5*7 * a % 3 = 1; --------------> a = 2; that is, 5*7*2 = 70;

3*7 * B % 5 = 1; --------------> B = 1; that is, 3*7*1 = 21;

3*5 * c % 7 = 1; --------------> c = 1; that is, 3*5*1 = 15;

Why do we need to set the remainder to 1: If the remainder 2 is required, multiply it by 2. If the remainder is 3, multiply it by 3!

(Because the question requires 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, (3*7*1) * 3% 5 = 3; (because 3*7*1% 5 = 1)

Similarly: To make n % 7 = 2, (3*5*1) * 2% 7 = 2; (because 3*5*1% 7 = 1)

So what if we add (5*7*2) * 2 and (3*7*1) * 3 and (3*5*1) * 2? 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

Even after the sum, % 3, 5, and 7 are the same!

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

But not the smallest! We still need 233% (3*5*7) = 23 solutions!

 


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

// The above is the algorithm parsing. It seems that the explanation is not very clear. Sorry ~

 


Now let's take a look at this question: x % 23 = p; x % 28 = e; x % 33 = I; x

Follow the above algorithms:

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

Make 23*33 * B % 28 = 1, get B = 19; 23*33*19 = 14421;
Make 23*28 * c % 33 = 1, c = 2; 23*28*2 = 1288.

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

Then x-d is the number of days of the difference!

Because of the range limit, (x-d) % = 21252; and If <= 0 at this time, then (x-d) + = 21252, all of the above is to ensure that they are in the scope ~

The AC code is as follows:


[Cpp] view plaincopyprint?
/*
China Surplus theorem: from Sun Tzu's computing Sutra

*/
 
# Include <stdio. h>
 
# Deprecision MAX 21252
 
Int main ()
{
Int p, e, I, d, n, count = 0;

While (scanf ("% 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) // range limit
{
N + = 21252;
}

Printf ("Case % d: the next triple peak occurs in % d days. \ n", count, n );
}
Return 0;
}

/*
China Surplus theorem: from Sun Tzu's computing Sutra
 
*/

# Include <stdio. h>

# Deprecision MAX 21252

Int main ()
{
Int p, e, I, d, n, count = 0;
 
While (scanf ("% 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) // range limit
{
N + = 21252;
}

Printf ("Case % d: the next triple peak occurs in % d days. \ n", count, n );
}
Return 0;
}


 

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.