Problem 854
Water and Jug Problem
Medium · 20 pts · Math, Number Theory, Breadth-First Search
Problem
You have two jugs with capacities X and Y litres and unlimited water. You may fill a jug completely, empty a jug, or pour from one jug into the other until the first is empty or the second is full. Decide whether you can end with exactly TARGET litres in the two jugs together. By Bézout's identity this is possible exactly when TARGET <= X + Y and TARGET is a multiple of gcd(X, Y).
Input
A single line with three integers X, Y and TARGET.
Output
Print YES if TARGET litres can be measured, otherwise NO.
Constraints
1 <= X, Y <= 10^6; 0 <= TARGET <= 2 × 10^6
Example input
3 5 4
Example output
YES
Explanation
With jugs of 3 and 5 litres, fill the 5, pour into the 3 (2 left), empty the 3, pour the 2 in, fill the 5 and top up the 3: 4 litres remain in the 5-litre jug. So for TARGET = 4 the answer is YES.