What is the optimal way to wash a dirty vessel with a fixed amount of water and time?

You want to pour in the same amount $\frac WT$ of water each time.

Each time you pour in $z$ water, mix and dump, the concentration is reduced by a factor $\frac D{D+z}$. Because multiplication is commutative, it doesn't matter what order you put the volumes in, just the list of volumes you use.

The easiest way to see it is to let $T=2$. We might as well measure the water in fractions of $W$, which makes $W=1$. Let us pour in $x$ the first time and $1-x$ the second time. After the first time you have volume $D$ left with concentration $\frac D{D+x}$. After the second you have volume $D$ left with concentration $\frac D{(D+x)(D+1-x)}$ To have the best cleaning we want to maximize $(D+x)(D+1-x)=D^2-x^2+D+x$. The usual technique maximizes this at $x=\frac 12$. This shows that given two unequal pours, the final concentration will be reduced by making them equal. Extending this, we want all the pours to be equal.


Suppose you perform a cleaning operation using $X$ water. Then the total fluid inside the cup is $X + D$. After pouring out the excess, the remaining amount inside the cup is $D$, so the ratio of milk inside at the beginning to milk inside at the end is equal to $\frac{X+D}{D}$, or $\frac{X}{D}+1$.

Suppose your cleaning operations use water amounts $X_1, X_2, \ldots, X_T$ which sum up to $W$.

Then the ratio of milk inside at the beginning to milk inside at the end is exactly

$$(\frac{X_1}D+1)(\frac{X_2}D+1)\cdots(\frac{X_T}D+1).$$

To maximize this ratio, we need all these $X_i$ to be equal, because if two were unequal, we could replace both by their average to get a better ratio (see later). If all the $X_i$ are equal, then they're all equal to $W/T$.

So the best method to clean the cup will cause the amount of milk at the end to be

$$\frac{D}{\left(1+\frac WD\cdot\frac 1T\right)^T}.$$

Note that as you make $T$ larger and larger, the bottle will get cleaner and cleaner, but as $T$ goes to infinity the bottle won't become perfectly clean. There's a limit to how clean the bottle can be, and the minimal remaining amount of milk is equal to $$De^{-\frac WD}.$$

(To pass that limit, just use some more water.)


To elaborate on why using equal amounts of water each time is the best approach given fixed total water and operations:

Suppose you use two different water amounts $X$ and $Y$ to clean two times.

Then the cleaning ratio is equal to $(\frac XD + 1)(\frac YD+1) = \frac {XY}{D^2} + \frac{X+Y}D + 1$.

But now suppose you split the water you used evenly instead, by cleaning with the amount $\frac 12(X+Y)$ twice. That uses the same amount of water in total, but the new cleaning ratio is

$(\frac {X+Y}{2D} + 1)^2 = \frac{(X+Y)^2}{4D^2} + \frac{X+Y}{D} + 1$.

Okay, what's the difference? The difference between the new and old cleaning ratio is exactly

$$\frac{(X+Y)^2-4XY}{4D^2} = \frac{X^2+2XY+Y^2-4XY}{4D^2} = \frac{X^2-2XY+Y^2}{4D^2} = \frac{(X-Y)^2}{(2D)^2}.$$

The numerator and denominator are both the square of a nonzero number, so the difference is indeed positive. Hence the new cleaning ratio is better, and averaging our water amounts between both cleaning cycles did actually help.