The Array Walk
Practice
5 (1 votes)
Extended euclid
Greatest common divisor
Medium Hard
Mathematics
Open
Approved
Problem
14% Success 1952 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There is an infinite one dimensional array ranging from (-infinity, infinity). You are currently at cell number 0. Your goal is to reach cell number H. There are two kinds of moves allowed.

  • Make U steps to the right (positive side).
  • Make D steps to the left (negative side).
Your task is to find the minimum number of moves to reach your goal.

Input & Output:

The first line contains the number of test cases T. Each of the next T lines contains 3 space separated integers, H U D. For each test case, output one line with an integer, the minimum number of moves required to reach H from 0. If it is impossible , print -1 instead.

Constraints:
T ≤ 10000
1 ≤ H, U, D ≤ 109

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
72 votes
Tags:
ReadyBasic ProgrammingMedium-HardGreatest common divisorMathematics
Points:50
1 votes
Tags:
Number TheoryAlgorithmsGCDMathGreedy algorithm
Points:50
47 votes
Tags:
Medium-HardMobius InversionNumber TheoryMathematicsGreatest common divisor