MAY99_3 - Easy Jug

no tags 

One day Manku was very thirsty , so he decided to drink exactly z litres of water .
However , in front of him, there is a well of infinite amount of water and 2 empty jugs of quantity x litres and y litres respectively.

Now Manku can do the following operations to any jug

1> Fill it completely from the well
2> Empty it entirely
3> Transfer as much water from Jug 1 to Jug 2 , till Jug 1 gets empty or Jug 2 is completely filled.

Now since he has no measuring device so he will do these operations only to make any of the 2 jug having exactly z litres of water.

Now Your task is given value of x,y,z , tell whether it is possible for Manku to drink water or not.



First Line of Input contains t , no. of test cases. (t<=25)

Then for each test case there are 3 no's x,y,z given in separate line 






For each test case output  "YES" if manku can drink exactly z litres of water else "NO".


2 4 3
2 5 1
9 3 6
3 8 7
6 1 10

 Output: NO


In Test case 1 Either Manku can have 2 or 4 litres of water so he cant drink 3 litres

In Test case 2 Manku can have 1 litre water by doing the following operations

 -> Fill 2 litre Jug

-> Transfer its water to 5 litre Jug

-> Again Fill 2 litre Jug

-> Again Transfer its entire water to 5 litre Jug
Now 5 litre Jug will have total 4 litre water

->Again Fill 2 litre Jug

-> Now transfer 1 litre water to 5 litre Jug

because at present 5 litre Jug don't have space for more than 1 litre water
Now the 2 litre Jug will have only 1 litre water left

For Test case 3 we will transfer 3 litre water twice from 3 litre jug to 9 litre jug

For test Case 4 , transfer 3 times water of 3 litre jug to 8 litre jug
Ultimately 3 litre Jug has 1 litre water left and 8 litre Jug is full
Now empty 8 litre jug and pour remaining 1 litre of 3 litre jug in it
Now fill 3 litre jug fully twice and transfer its water to 8 litre jug

Now 8 litre Jug will have 7 litre water

For Test case 5, we cant have 10 litre of water in any jug


hide comments
lakshay_v06: 2016-01-25 08:50:28

Prerequisites : Diophantine equation and Extended euclids algo. :)

thedictator: 2015-12-10 13:49:35

too easy if the logic strikes in your mind

bunnycoder: 2015-10-20 17:16:44

AC in one go had to think a lot about the logic

rahul_verma: 2015-09-26 19:55:41

same as CEQU just copy the code and change output format got AC!!!

Kaustubh Mallik: 2015-09-26 15:09:59

@hello_word @Kushal Saharan: Read the question carefully. It says you should have "z" litres of water in one of the jugs at the end, from where Manku has to drimk. The second last statement of the problem has it. "Now since he has no measuring device so he will do these operations only to make any of the 2 jug having exactly z litres of water." You should read the problem statements carefully rather than blaming the judge or saying the incorrectly framed question.

geoffreymace7: 2015-08-16 15:28:45


rrv: 2015-07-16 19:27:57

remember you have to drink only once from any one of the jugs n any one of them must be able to hold z litres of water

mrx: 2015-07-06 16:20:43

lol, no special logic required :P

aparna00: 2015-06-09 20:13:11

does it mean dat manku can drink from only one of the two glasses ? otherwise ans for 6 4 10 wud have been a YES !!

vagesh_verma: 2015-06-05 10:46:26

easy jug.....easy one

Added by:Mayank Tuteja
Time limit:0.187s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:ASMGCC clang_c C clang_cpp C++ 5 C++ 4.3.2 C++14 COB dmd clang_d elixir fantom GROOVY JAVA JS2 nim clang_objc objc pico PYPY rust SCM chicken