Two Buckets

Problem #274

Tags: c-1 classical puzzle

Who solved this?

No translations... yet
supposedly medieval image of monk tasting wine from a barrel
Supposedly medieval French image of the monk tasting wine

That's quite an old puzzle. Just let's add some automation to it!

Brother Sobertino has a task to examine certain barrel of wine in the cellar - whether it is good enough for the upcoming feast or not.

Barrel is very large, and Sobertino got two buckets with him, with volumes of V and U gallons. However, since he is not a drunkard, he only wants to measure small amount of T gallons for examination. The goal is to get exactly this amount by the following manipulations with the buckets:

For example, if he has buckets of 3 and 5 gallons, and wants to measure out just 1 gallon, he can go this sequence:

So it takes 4 operations to measure 1 gallon with two buckets of 3 and 5.

Problem Statement

Please find the minimal number of operations to get T gallons with buckets of U and V gallons.

Input data: just three values, U, V and T in a single line.
Answer should be a single value - required minimum number of operations.

Example:

input:
7 4 2

answer:
8
You need to login to get test data and submit solution.