Two water jugs
InterviewTime limit2sMemory limit512 MB
Given two jug capacities a and b, decide whether some sequence of fills, empties, and pours can leave exactly c liters in one jug.
- Level
Medium5 of 10
- Topics
- Math, Number theory, Simulation
- Solved
- No attempts yet
Problem
You have two jugs with capacities of and liters. Water is unlimited, and you may repeat the following three operations as many times as you like.
- Fill one jug to the top.
- Empty one jug completely.
- Pour water from one jug into the other, stopping when the source jug is empty or the receiving jug is full.
Write a program that decides whether you can reach a state where one of the two jugs holds exactly liters.
Input
The first line contains the number of test cases . ()
Each of the next lines contains one test case, given as three integers , , . ()
Output
For each test case, print YES on its own line if exactly liters can be measured, and NO otherwise.
Hint
For , , , the following steps produce 4 liters.
- Fill . (, )
- Pour into . (, )
- Empty , then pour into . (, )
- Fill . (, )
- Pour into . (, )