Making a number on the counter
Statement
A science museum has a counter on display that shows one number. The counter has two buttons, and pressing a button changes the number on the display as follows.
| Button | What it does | When the display shows 23 |
|---|---|---|
| Left | The number on the display doubles | 46 |
| Right | One 1 is attached to the right of the number on the display | 231 |
The display now shows the number A. The buttons are to be pressed to turn the display into the number B. What gets counted is how many numbers pass across the display, and both the A that was there at the start and the B that is made at the end are counted. Each press brings up one more number on the display, so the count of numbers that pass is one more than the number of presses.
For example, suppose A=2 and B=162. Pressing the buttons four times as follows makes 162.
| Button pressed | Start | Left | Left | Right | Left |
|---|---|---|---|---|---|
| Number on the display | 2 | 4 | 8 | 81 | 162 |
The numbers that pass across the display are the five numbers 2, 4, 8, 81 and 162. No way makes 162 with fewer presses, so the answer in this case is 5.
When A and B are equal, no button is pressed. The only number that passes across the display is the A from the start, so the answer is 1.
Sometimes B cannot be made. If A=2 and B=3, the left button gives 4 and the right button gives 21, and both overshoot 3. The number on the display only grows with each press, so there is no going back either. The answer in such a case is -1.
Working backward from B is another way to look at it. When B is even, the number before the doubling is B divided by 2, and when the ones digit of B is 1, the number before the 1 was attached is B with its ones digit taken off. Working backward from B=162 gives the following.
| Step | Number on the display | How we went backward |
|---|---|---|
| 1 | 162 | start |
| 2 | 81 | even, so divided by 2 |
| 3 | 8 | ones digit is 1, so took it off |
| 4 | 4 | even, so divided by 2 |
| 5 | 2 | even, so divided by 2 |
Reaching A this way makes the count of numbers passed up to then the answer. Landing on a number that is neither even nor ends in 1, or dropping below A, means B cannot be made.
Given A and B, write a program that finds how many numbers pass across the display when B is made with as few presses as possible. When B cannot be made, find -1.
Input and output examples
Example 1
Input
2 162
Output
5
Example 2
Input
1 1
Output
1
Example 3
Input
2 3
Output
-1
Input
The first line gives the number A on the display at the start and the number B that has to be made, separated by one space. A and B are both natural numbers from 1 to 1,000,000,000. A is less than or equal to B.
The input ends with this one line. There is no limit on how many times the buttons can be pressed. Cases where A and B are equal are given as well.
Output
Print the count of numbers that pass across the display when B is made with as few presses as possible on the first line. When B cannot be made, print -1.
Print one number and then a newline. Do not add any other character or space before or after it.
Requirements
- Read the input from standard input and write the answer to standard output
- The value counted is not the number of presses but the count of numbers that pass across the display. Both the A at the start and the B at the end are counted. The answer is therefore one more than the number of presses
- When A and B are equal, no button is pressed, so the answer is 1
- The right button attaches a 1 to the right of the number. It does not add 1
- When B cannot be made, print -1
- Grading compares the output with the expected value down to the last character
- 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.
- 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.
2 162
5
1 1
1
2 3
-1
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
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.