HDU 1415 jugs

Source: Internet
Author: User

The so-called irrigation problem, ah, ah, why is this question in the search courseware, I have been depressed for a long time

Below is a link to the materials and my ownCode

Http://blog.csdn.net/lipengyuan_fan/article/details/4369709

The typical form of water diversion is as follows:

"Suppose there is a pond with an infinite amount of water in it. There are two empty kettles with 5-litre and 6-litre capacities respectively. The question is how to get 3 litre water from the pond with only the two kettles ."

Of course, there are some reasonable restrictions. For example, when watering from the pond, whether there is water in the pot or not, the pot must be filled with water, you cannot compare the water level in another pot with the "gross estimate" (we can assume that the pot is not transparent and has different shapes). Similarly, if you want to pour water from the pot into the pond, you must pour all the light; if you want to pour water from one pot into another, you must also pour all the light, unless another pot is full during the pouring process, there is no loss of water (evaporation overflows or something) during pouring, and so on.

In fact, to solve this problem, you only need to use one of the two pots to fill the pond and continuously pour it into another pot. When the second pot is full, pour the water back to the pond, and then get the answer several times. Taking 5-litre pot (a) and 6-litre pot (B) as an example:

A B
0 0
5 0 A → B
0 5
5 5 A → B
4 6
4 0 A → B
0 4
5 4 A → B
3 6

Now, what should we do if there are more than two pots (in this way, we cannot use the circular water pouring method )? Before pouring water, how can we know that these pots can (or must not) pour out a number of liters of water? Examples:
1) Two POTS: 65 and 78, 38 and 39.
2) Three pots: 6 litre, 10 litre, 45 litre, 31 litre.

In 1), 65 = 5 × 13, 78 = 6 × 13, and 39 = 3 × 13. So if you think of 13 liters of water as a unit (the "litre" in the original question is of no significance, you can replace it with any volume unit, ML, gallon-or "13 litre"), which is the same as the original question. What about 38 litre? Obviously, it is impossible. It is not a multiple of 13, and the 65-litre and 78-litre Pots can only output a multiple of 13-litre. It can also be understood as follows: in the original question, the 5-litre and 6-litre pots are required to generate 38/39-litre pots.

SO 2? You will find that only two of the pots can not pour 31 liters of water, the reason is as mentioned above, (6, 10) = 2, (6, 45) = 3, (10, 45) = 5, (Here (A, B) is the maximum common divisor of A and B), while and 5 do not divide 31. However, you can use three pots to pour 31 liters: Use a 10-litre pot for four times, and fill a 6-litre pot for 45 liters at a time to get 1 liter of water, then fill the pot with 10 liters of water three times to get 30 liters of water, add up to 31 liters.

Generally, we have the "irrigation Theorem ":

"If there are n pot capacities A1, A2 ,......, An (All integers whose AI values are greater than 0) is set w to another integer greater than 0. You can use the N pots to pour out the following conditions for W-litre water:
1) W is less than or equal to A1 + A2 +... +;
2) W can be divisible by (A1, A2,..., an) (the maximum number of N ."

Both of these conditions are obviously necessary. If 1) It is not met, you will not have to put so much water. 2) The truth is exactly the same as that of the above two pots, because in any step, any pot will always be (A1, A2 ,......, an.

Now let's look at the adequacy. In middle school, we learned that if two integers A and B are mutually unique, there are two integers U and V, making UA + vb = 1. The proof method is very simple: when we perform Euclidean Division on a and B, all intermediate results, including the final results, are obviously in the form of UA + vb (for example, the first step, assume that A is less than B, and the result of dividing a by B is S, and the remainder is T, that is, B = SA + T, then t = (-S) A + B, that is, u =-S, V = 1 ). The result of the division of two numbers means that the last step of Euclidean division is 1. Therefore, 1 can also be recorded as UA + VB. A little more, if (a, B) = C, then u and v make UA + vb = C (both sides are divided by C and return to the original proposition ).

To promote this, if A1, A2 ,......, An is n integers (A1, A2,..., an) = s, so there are integers u1, U2 ,......, UN, making

U1a1 + u2a2 +... + UNAN = S .(*)

The result is called "an integer ring is the main ideal ring" in the mathematical notation ". This is not hard to prove, as long as you see

(A1, A2, A3, A4 ,......, an) = (A1, A2), A3), A4 ),......, an ).

That is to say, the formula in the previous section can be applied repeatedly: for example, the maximum number of three numbers A, B, and C is D. Suppose (a, B) = E, then (E, C) = (a, B), c) = D. Now we have u1, U2, u1a + U2B = E, V1, V2, and v1e + v2c = D.

(V1u1) A + (v1u2) B + (V2) C = D.

Well, let's look back at "irrigation Theorem ". W is (A1, A2 ,......, an) Multiple. Based on the formula (*) in the preceding section, multiply the two sides by this multiple. We have the integers V1, V2 ,......, V1a1 + v2a2 +... + vnan = W. Note that VI is positive and negative.

This indicates that we only need to separate A1, A2 ,......, An pot, fill in V1, V2 ,......, Vn times (if VI is negative, "fill VI times" should be understood as "Empty-VI times"), you can get W liters of water. For specific operations, first find the VI, and then first fill the pot where VI is a positive number, fill the pot once and then reduce VI by 1. Add the water to the pot where VI is negative. When a pot is filled, empty it and Add 1 to the negative VI, do not change the values of each VI. It should be noted that it is necessary to fill the water in the pond with an empty pot. It is necessary to fill the whole pot with water in the pond. In this way, until all VI are 0.

Will it be stuck, neither irrigation nor dumping? No. If a VI is still negative, but the AI pot is not full: if there is water in the other VI is positive pot, pour it; if there is no water in the pot where other VI is positive, fetch water from the pot (do not forget to subtract 1 from the VI of the pot ); if no VI is a positive pot at all -- this is impossible, which means W is negative. If VI is still a positive number, but the AI pot is not full, it is similar. You will find that you need to use condition 1 in the theorem ).

In this way, the "irrigation Theorem" is proven completely. Of course, in practice, if the number and volume of pots are relatively large, it is difficult to manually find the UIS in (*), but you can writeProgram, Even the steps for pouring water are calculated. The last point to be pointed out is that the UI in (*) is not unique, so the method of pouring water is not unique.

View code

 # Include  <  Iostream  >  
Using Namespace STD;
Int Main ()
{
Int CA, CB, N, VA, VB;
While (CIN > CA > CB > N)
{
If (CB = N)
{Cout < " Fill B " < Endl;
}
Else If (CA = N)
{
Cout < " Fill " < Endl;
Cout < " Pour a B " < Endl;
}
Else
{
VA = VB = 0 ;
If (CA < N)
{
While ( 1 )
{ If (VB = 0 )
{
VB = CB;
Cout < " Fill B " < Endl;
}
If (VB > CA - Va) // If the water in B is larger than the remaining volume in a, fill a //
{
VB -= CA - Va;
VA = CA;
Cout < " Pour B " < Endl;
}
Else // If the water in B is less than the remaining volume in A, add all the water in B to. //
{
VA + = Vb;
VB = 0 ;
Cout < " Pour B " < Endl;
}
If (VB = N) // If the water in B is equal to CC, it will end. //
{
Break ;
}
If (CA = Va) // If the water in a is full, empty. //
{
VA = 0 ;
Cout < " Empty " < Endl;
}
}
}
Else
{
While ( 1 )
{
If (VA = 0 )
{
VA = CA;
Cout < " Fill " < Endl;
}
If (VA > CB - VB) // If the water in a is larger than the remaining volume in B, fill B //
{
VA -= CB - Vb;
VB = CB;
Cout < " Pour a B " < Endl;
}
Else // If the water in a is less than the remaining volume in B, add all the water in A to B. //
{
VB + = Va;
VA = 0 ;
Cout < " Pour a B " < Endl;
}
If (VB = N) // If the water in B is equal to CC, it will end. //
{
Break ;
}
If (VB = CB) // If the water in B is full, empty B. //
{
VB = 0 ;
Cout < " Empty B " < Endl;
}
}
}
}
Cout < " Success " < Endl;
}
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.