571-jugs
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=512
Ideas:
Since it is special judge, it is possible to construct a feasible solution.
Conclusion: If A is empty, add water, not empty to B pour, b full after the empty off, so in B can certainly form a 0~b any solution.
Proof: In this way to pour water, we can use (k*a)%b to indicate the amount of water in B, because of A and B coprime, so the LCM (a,b) =ab, then A accumulation of B can return to the starting point, so the minimum period is B. And, in a cycle, we can use contradiction to prove that all the values are different (as long as the last value and the previous one is not available), so we can certainly get a sequence of operations that satisfies the requirements.
Complete code:
/*0.029s*/
#include <cstdio>
int main ()
{
int na, NB, N, CA, CB;
while (~SCANF ("%d%d%d", &ca, &CB, &n))
{
na = 0, nb = 0;
while (NB!= N)
{
if (Na = = 0)
{
puts ("Fill A");
NA = CA;
}
if (NB = = cb)
{
puts ("Empty B");
NB = 0;
}
NB = na;
Puts ("Pour A B");
if (NB > cb) na = NB-CB, NB = cb;
else na = 0;
}
Puts ("Success");
}
return 0;
}
Author: csdn Blog Synapse7
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/