#P1292. Minimal Remnant in Beer Cup
Minimal Remnant in Beer Cup
Minimal Remnant in Beer Cup
Winy owns a bar that serves beer in two sizes using two types of cups: one with a capacity of \(a\) ml and the other with a capacity of \(b\) ml (with \(a \ge b\)). Due to business issues, he wants to sell a third, smaller portion of beer. However, he only has these two cups (without any graduations) and an infinitely large barrel of beer.
Winy can only perform the following pouring operations:
- Fill the \(b\) ml cup from the barrel (the cup is filled to \(b\) ml).
- Empty the \(a\) ml cup into the barrel (the cup is completely emptied).
- Pour from the \(b\) ml cup into the \(a\) ml cup. In this operation, you must either empty the \(b\) ml cup or fill the \(a\) ml cup completely.
Starting from both cups empty, Winy wants to perform a sequence of these operations so that the remaining amount of beer in the \(a\) ml cup becomes as small as possible (but nonzero), in order to serve a smaller portion. It turns out that the minimum positive amount that can be measured in the \(a\) ml cup is equal to \(\gcd(a, b)\). Your task is to compute this minimal measurable volume in the \(a\) ml cup.
Note: All formulas are provided in \(\LaTeX\) format.
inputFormat
The input consists of two space-separated integers \(a\) and \(b\) (in ml) on a single line, with the condition \(a \ge b\).
outputFormat
Output the minimum measurable volume (in ml) that can remain in the \(a\) ml cup using the allowed pouring operations (i.e. \(\gcd(a, b)\)).
sample
9 6
3