Setting the dial
Statement
A signal generator in a lab has one dial on it, and the dial carries marks numbered from 0 to 100,000. The dial is set to mark N right now, and the experiment needs it set to mark K.
There are only three ways to move the dial. When the current mark is X, the mark each way moves to is shown in the table below.
| Way | Down by 1 | Up by 1 | Double |
|---|---|---|---|
| Mark moved to | X-1 | X+1 | 2X |
Any one of the moves takes 1 second. None of the three ways can reach a mark below 0 or a mark above 100,000. A move that would land on such a mark counts as unusable. A mark that has already been visited may be passed through again, and the dial may overshoot the target mark and come back.
Write a program that computes the shortest time it takes to set the dial from mark N to mark K.
For example, suppose N=5 and K=17. The four moves below reach mark 17.
| Time | 0 seconds | 1 second | 2 seconds | 3 seconds | 4 seconds |
|---|---|---|---|---|---|
| Way used | None | Down by 1 | Double | Double | Up by 1 |
| Mark | 5 | 4 | 8 | 16 | 17 |
Doubling straight from mark 5 gives mark 10, turning up by 1 from there gives mark 11, and doubling again gives mark 22, which overshoots mark 17. Turning down by 1 from there costs five more moves. No way of reaching mark 17 takes three moves or fewer. The answer for this example is therefore 4 seconds.
Input and Output Examples
Example 1
Input
5 17
Output
4
Example 2
Input
1 1
Output
0
Example 3
Input
1 3
Output
2
Input
First the mark N the dial is set to right now and the mark K it has to be set to are given on one line, separated by a space. N and K are each a natural number from 1 to 100,000. N and K may be equal. The marks the dial can pass through run from 0 to 100,000, so passing through mark 0 along the way is allowed. The input ends with this one line.
Output
Print the shortest time it takes to set the dial from mark N to mark K as a number of seconds on one line. If the dial is already set to mark K and does not need to move, print 0.
Requirements
- One move can turn the mark down by 1, up by 1, or to twice its value
- Any one of the ways takes 1 second per move
- The dial cannot reach a mark below 0 or a mark above 100,000
- Print only the single shortest time it takes
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
5 17
4
1 1
0
1 3
2
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.