TITLE: The milk sharing problem
QUESTION [31 upvotes]: I found a book with math quizzes. It was my father's when he was young. I encountered a problem with the following quiz. I solved it, but I wonder, is there a faster way to do it? If so, how can I compute the time (polynomial time) that is needed to solve it? Can we build an algorithm? 
The problem is this one:
Two brothers have a cow that produces 10 kilos of milk per day. Because they are fair, they share the milk and each one gets 5 kilos.
They only have available three bottles.
$A$ that fits 10 kilos.
$B$ that fits 7 kilos.
$C$ that fits 3 kilos.
How do they share it?
What I did is these steps:

   Bottle Sizes
      A   B   C
     10   7   3

Moves    Bottles        What we do:
 1st    10   0   0      Fill the bottle A.
 2nd     7   0   3      Fill the bottle C.
 3rd     7   3   0      Empty the index of C in B.
 4th     4   3   3      Refill C.
 5th     4   6   0      Empty C in B.
 6th     1   6   3      Refill C from A.
 7th     1   7   2      Refill B from C.
 8th     8   0   2      Empty the index of B in A.
 9th     8   2   0      Empty the index of C in B.
10th     5   2   3      Refill C from A.
11th     5   5   0      Empty C in B.

REPLY [4 votes]: These problems are routinely solved by using the Euclidean algorithm. The greatest common divisor of $7$ and $3$ is $1$, and this number can be written as an integral combination of $7$ and $3$ (Bezout's identity). In fact, here we can immediately write
$$1\cdot 7-2\cdot 3=1$$
Multiply through by $5$, and we get
$$5\cdot 7-10\cdot 3=5$$
This gives one possible solution: Fill the medium ($7$ kg) bucket five times, and use the smallest bucket to remove $3$ kg ten times. This is not very efficient, but we can produce new solutions by adding and subtracting the identity
$$3\cdot 7 - 7\cdot 3=0$$
as many times as we wish. Subtracting it once, we get
$$2\cdot 7-3\cdot 3=5$$
This shows a much more efficient solution, where we fill the medium bucket twice, and use the smallest bucket to remove $3$ kg three times. We can subtract the identity $3\cdot 7-7\cdot 3=0$ once more, resulting in
$$(-1)\cdot 7 + 4\cdot 3=5$$
This gives a solution where we fill the smallest bucket four times, and use the medium bucket to remove $7$ kg.
There are infinitely many more ways of writing $5$ as an integral combination of $7$ and $3$, but these two are the ones with coefficients closest to zero, so they are the only candidates for solving the problem.
Since we have a lack of large containers, there remains the practical problem of implementing these solutions without spilling any milk. This has to be done by pouring back and forth between the buckets that we have. This can always be done, and at any stage of the process there is only one way to proceed in accordance with the solution that we wish to implement. Looking at the solution given by the OP in the problem statement, we see that we have filled the $7$ kg bucket twice, and we have poured $3$ kg from the medium to the smallest bucket four times, which makes this the implementation of the solution $2\cdot 7-3\cdot 3=5$. Looking at the lower solution by @Xoff, we see the implementation of the solution $(-1)\cdot 7+4\cdot 3=5$, where we have poured $3$ kg into the medium bucket four times, and removed $7$ kg once. Comparing these two alternatives, as in several of the answers above, we get that the solution given by the OP is in fact the optimal one.