Ultraviolet A 571 Jugs

Source: Internet
Author: User

UVA_571

There was no idea at first, and later I came to the others' report and found that they were done in a greedy way.

Because this question does not limit the number of times of pouring water, we only need to construct A feasible solution. If we add water every time A is empty, we will pour water to B, when B is full, empty is dropped. In this way, any solution between 0 and B can be formed in B.

Later, I proved it easily. If we pour water in this way, we can use (n * A) % B to represent the amount of water in B. Because of the mutual quality of A and B, it is easy to get that the minimum cycle of this function is B, and in a period, we can easily prove that the values of this function are different, and the values of n in a period (accurately two critical points must be included) are 0-B, then the corresponding value will inevitably overwrite 0-B, so we can get an operation sequence that meets the requirements.

#include<stdio.h>
#include<string.h>
int Ca, Cb, N;
void solve()
{
int i, j, k, a, b;
a = b = 0;
for(;;)
{
if(b == N)
{
printf("success\n");
break;
}
else if(b == Cb)
{
printf("empty B\n");
b = 0;
}
if(!a)
{
printf("fill A\n");
a = Ca;
}
printf("pour A B\n");
if(a + b > Cb)
{
a = a + b - Cb;
b = Cb;
}
else
{
b += a;
a = 0;
}
}
}
int main()
{
while(scanf("%d%d%d", &Ca, &Cb, &N) == 3)
{
solve();
}
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.