CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(medium) » Pouring water

Pouring water

Problem code: POUR1

  • Submit
  • All Submissions

All submissions for this problem are available.


Given two vessels, one of which can accommodate a liters of water and the other which can accommodate b liters of water, determine the number of steps required to obtain exactly c liters of water in one of the vessels.


At the beginning both vessels are empty. The following operations are counted as 'steps':

  • emptying a vessel,
  • filling a vessel,
  • pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.

Input


An integer t, 1<=t<=100, denoting the number of test cases, followed by t sets of input data, each consisting of three positive integers a (the number of liters the first container can hold), b (the number of liters the second container can hold), and c (the final amount of liters of water one vessel should contain), not larger than 40000, given in separate lines.

Output


For each set of input data, output the minimum number of steps required to obtain c liters, or -1 if this is impossible.

Example

Sample input:

2
5
2
3
2
3
4

Sample output:

2
-1

Author: admin
Date Added: 1-12-2008
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

sagar_jhobalia @ 22 Jun 2009 07:12 PM

plz giv mre test examples....

there r only 3 cases case

gurpreet_09 @ 15 Sep 2009 07:32 PM

there r only 3 cases

case 1:

a==c or b==c then print 1

case 2:

a-b==c or b-a==c then print 2

case 3:

else print -1

 

 

 

 

 

if there r any more cases plz tell thnx.....

 

 

Suppose a=7 , b=5 and c=6

ashishanand @ 15 Sep 2009 08:16 PM

Suppose a=7 , b=5 and c=6 then for that case you will have following steps

a b
0 0
0 5
5 0
5 5
7 3
0 3
3 0
3 5
7 1
0 1
1 0
1 5
6 0

or for the above case the

ashishanand @ 15 Sep 2009 08:19 PM

or for the above case the better way will be

 a    b

 0   0
 7   0
 2   5
 2   0
 0   2
 7   2
 4   5
 4   0
 0   4
 7   4
 6   5

plz explain for the case

mij @ 6 Oct 2009 06:44 PM

plz explain for the case when

a=5,b=6,c=3;

0 0 0 6 5 1 0 1 1 0 1 6 5 2 0

vikram.gaur @ 12 Oct 2009 02:50 PM

0 0

0 6

5 1

0 1

1 0

1 6

5 2

0 2

2 0

2 6

5 3

 

or

0 0

5 0

0 5

5 5

4 6

4 0

0 4

5 4

3 6

Hi, if we take a=5 b= 6 c=3

illusionist @ 2 Mar 2010 10:50 AM

Hi,

if we take a=5 b= 6 c=3 then my output is 8 steps but if a=6 b=5 c=3 then my output steps are 10 and this goes for all combinations i am following the algo

Code:

while A != C && B != C
if A is empty fill A
else if B is full empty B
else pour from A to B
wend

i am getting wrong answer when i submit my solution so i just wanna confirm is this the reason?????

Well the answer for those two

triplem @ 2 Mar 2010 11:05 AM

Well the answer for those two test cases obviously should be the same. If you aren't getting the same answer, then you must be wrong.

what will be the answer if

anu_kc @ 9 Apr 2010 11:34 AM

what will be the answer if any one of the a, b or c=0??

sorry, got it!!

anu_kc @ 9 Apr 2010 11:39 AM

sorry, got it!!

fed up with these

anu_kc @ 18 Apr 2010 12:33 PM

fed up with these WA's....someone plss help!!!!!

admin plss help!!! dnt knw

anu_kc @ 18 Apr 2010 12:40 PM

admin plss help!!! dnt knw where m i going wrong.....getting ryt answers on my system... :( :(

Iv written the

nirmaltech28 @ 19 Aug 2010 10:02 PM

Iv written the program..

Everything is fine in my output screen..

but when i submit here it shows Time limit exceeded. Can anybody tell me the basic mistakes for time limit exceeding in this program?

Admin, please help. My

kapilagarwal @ 20 May 2011 08:01 PM
Admin, please help. My solution id is 551097. I have tried various cases but it gives time limit exceeded. The algorithm I have used gives the correct answer in less than 1 second but here it gives time limit exceeded.

i cant understand the problem

ramanastudy @ 18 Aug 2011 11:16 PM
i cant understand the problem requirement.....Anyone pls post the clear explanation...pls

@admin can u please give some

arpitagarwal30 @ 16 Nov 2011 10:55 AM
@admin can u please give some more test cases. my solution id is 733599 . can u plz tell me what are the test cases that my solution is failing. i have compared the outputs of my solution with the other correct submissions and i am getting same outputs .

Is it necessary that all the

ankitdbst @ 30 Dec 2011 10:38 PM
Is it necessary that all the cases with c < max(a,b), the answer would not be -1?

Can anyone tell a case with c

ankitdbst @ 30 Dec 2011 10:50 PM
Can anyone tell a case with c < a and c < b, and answer is -1? Thanks.

^^ if a=6 ,b=4 and c=3 then

rohitkr597 @ 12 May 2012 11:22 AM
^^ if a=6 ,b=4 and c=3 then answer =-1

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com