Making a number with four buttons
Statement
An old calculator on display in a museum has only four buttons. One number sits on the display, and whichever button is pressed, the calculator combines the number on the display with itself and puts the result back on the display. When the number on the display is X, what the four buttons do is shown in the table below.
| Button | Character | Number on the display after the press |
|---|---|---|
| Times | "*" | X squared |
| Plus | "+" | X doubled |
| Minus | "-" | X minus X, which is always 0 |
| Divide | "/" | X divided by X, which is always 1 |
The divide button cannot be used when the number on the display is 0. The number that can be put on the display is also 1,000,000,000 or less. If a button press puts a number larger than 1,000,000,000 on the display, no further button can be pressed after that, and the goal cannot be met from that state. For example, when the number on the display is 100,000, pressing the times button gives 10,000,000,000, which goes over the limit, so there is nothing more to try from there.
The number on the display at the start is s, and the goal is to put t on the display. Several ways may tie for the fewest presses. In that case the characters of the buttons rank in the order "*", "+", "-", "/", with the earlier character ranking first, and the one that comes first in lexicographic order is taken as the answer. So if a times press followed by a plus press and a plus press followed by a times press both meet the goal in the same number of presses, the one whose leading character ranks earlier is taken as the answer.
Write a program that makes the number of button presses as small as possible and prints the buttons pressed, written out in order.
For example, suppose s=12 and t=2. Pressing the divide button puts 12 divided by 12, which is 1, on the display, and pressing the plus button after that puts 1 doubled, which is 2, on the display.
| Presses made | 0 | 1 | 2 |
|---|---|---|---|
| Button pressed | None | Divide | Plus |
| Number on the display | 12 | 1 | 2 |
No single press puts 2 on the display. 12 squared is 144, 12 doubled is 24, minus gives 0, and divide gives 1. The answer for this example is therefore "/+", the characters of the buttons pressed joined in order.
Input and Output Examples
Example 1
Input
12 2
Output
/+
Example 2
Input
2 2
Output
0
Example 3
Input
3 0
Output
-
Example 4
Input
5 125
Output
-1
Input
First the number s on the display at the start and the target number t are given on one line, separated by a space. s is a natural number from 1 to 1,000,000,000, so 0 never sits on the display at the start. t is an integer from 0 to 1,000,000,000, and the minus button can put 0 on the display, so t may be given as 0. The two numbers may be equal. The input ends with this one line.
Output
Print the characters of the buttons pressed, joined in order with no spaces, on one line, for a sequence of presses whose count is the smallest. If several ways tie for the fewest presses, the characters of the buttons rank in the order "*", "+", "-", "/", with the earlier character ranking first, and the one that comes first in lexicographic order is printed.
If s and t are the same and no button needs to be pressed, print 0. If no sequence of presses can put t on the display, print -1.
Requirements
- Each button combines the number on the display with itself, so no other number can be entered
- The divide button cannot be used when the number on the display is 0
- Once a number larger than 1,000,000,000 sits on the display, no further button can be pressed
- If several ways tie for the fewest presses, print the one that comes first in lexicographic order, where the characters of the buttons rank in the order "*", "+", "-", "/"
- Print 0 when no press is needed, and -1 when the number 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 4 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
4 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
12 2
/+
2 2
0
5 125
-1
3 0
-
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 4 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.