Four Operations
Time limit2sMemory limit512 MB
Starting from s, apply +, -, *, / (each replacing s with s+s, s-s, s*s, or s/s) to reach t in the fewest steps, ties broken lexicographically.
- Level
Medium5 of 10
- Topics
- BFS, Math, Brute force
- Solved
- No attempts yet
Problem
You are given an integer . Write a program that finds the minimum number of operations needed to turn the value of into .
The available operations are:
s = s + s; (output: +)s = s - s; (output: -)s = s * s; (output: *)s = s / s; (output: /)(allowed only when is not 0)
Input
The first line contains two integers and separated by a space. ()
Output
On the first line, print a way to turn into as the string formed by concatenating the output characters of the operations in the order they are applied. Only a way with the minimum number of operations counts as an answer. If and are equal, print 0. If cannot be turned into , print -1.
If several ways use the minimum number of operations, print the lexicographically smallest one. In ASCII order the operation characters are *, +, -, /.