Faulty Mars Rover
InterviewTime limit2sMemory limit512 MB
Starting from a, reach exactly b using steps of +1 and +2 while never landing on a multiple of c; find the fewest steps.
- Level
Medium4 of 10
- Topics
- Math, Greedy, Implementation, Number theory
- Solved
- No attempts yet
Problem
A Mars rover carrying out an international mission on Mars has broken down.
To restore its operation, the rover's battery power must be increased. The battery power of the rover is given as a positive integer. The current battery power is , and to restore the rover's operation it must be increased to . To change the battery power, two types of special signals, X and Y, can be sent to the rover from Earth. A signal of type X increases the current battery power by 1, and a signal of type Y increases the current battery power by 2.
The mission organizers want to change the battery power to the required value by sending the minimum number of signals. Unfortunately, due to the rover's design, if the battery power becomes a multiple of the integer , the rover breaks down permanently and stops responding to signals.
Write a program that, given the initial battery power , the required battery power , and the integer , determines the minimum number of signals that must be sent to the rover to restore its operation.
Input
The input contains three integers , , and , one per line. (, , is not a multiple of , and is not a multiple of .)
Output
Print a single integer: the minimum number of signals that must be sent to the rover.
Hint
In the first example, one can proceed as follows: send the signals Y, X, Y to the rover. The battery power changes as . In the second example, one can proceed as follows: send the signals X, Y, X, Y to the rover. The battery power changes as .