Log inSign up
Problems / B087

Changing the safe's number

200 ptsCases 15Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

A safe has one natural number set on it as its number. The number set on it right now is A, and the owner of the safe wants to change this number to B.

Only the following two operations can be used to change the number one step at a time.

1) Double the number currently set.

2) Append one digit 1 to the right end of the number currently set.

Each time the number changes, the safe writes the number after the change into its logbook, one line at a time. The first line of the logbook holds A, the number that was set at the start. Once the number has been changed all the way to B, the logbook therefore holds the numbers in order, starting with A and ending with B.

The owner of the safe wants as few lines as possible left in the logbook. Write a program that takes A and B and finds the smallest number of lines left in the logbook. If the two operations alone cannot change A into B, print -1.

For example, suppose A=2 and B=162. Doubling 2 gives 4, doubling again gives 8, appending 1 on the right gives 81, and doubling last gives 162. Laying the steps out gives the table below.

Line of the logbook12345
Number written24881162
Operation usedDoubleDoubleAppend 1Double

The logbook is left with 5 lines in all. No way reaches 162 with fewer lines, so the answer in this case is 5.

Now look at the case A=4 and B=42. From 4 the only operations give 8 by doubling and 41 by appending 1, from 8 they give 16 and 81, and from 41 they give 82 and 411. The two operations only make the number larger, so once 42 has been passed there is no way back to it. No path gives 42 in the end, so the answer in this case is -1.

Input and Output Examples

Example 1

Input

2 162

Output

5

Example 2

Input

123 2461

Output

3

Example 3

Input

4 42

Output

-1

Input

  • First the number A currently set on the safe and the number B it is to be changed to are given on one line, separated by a single space. A and B are each a natural number from 1 to 1,000,000,000, and A is less than or equal to B. A case where the two numbers are equal may be given. The input ends with this one line, and no other line is given.

Output

  • Print the smallest number of lines left in the logbook on one line. A at the start and B at the end each count as one line, and if A and B are the same, print 1. If the two operations alone cannot change A into B, print -1. Only one number is printed, so no space is left at the end of the line.

Requirements

  • The only two operations are doubling and appending a 1 on the right
  • The answer is the number of lines left in the logbook, not the number of operations. A at the start and B at the end each count as one line
  • When A and B are the same no operation is carried out, so the answer is 1
  • The two operations only make the number larger, so there is no way back from a number that has passed B
  • Print -1 if the change cannot be made
  • 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.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
2 162
5
4 42
-1
123 2461
3

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.

0 chars1 lineMemory 256MB (recorded only)Reference 54ms

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.

1bampal228ms219 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.