Negative Ones
View as PDFYou are given an irreducible fraction, . Let the initial value of a variable
be
. There are three available operations on
:
+- Addto
.
*- Multiplyby
.
^- Raiseto the power of
.
Note that must be nonzero for operation type 3
^.
Output any minimum length sequence of operations which makes equal to
. If it is not possible, output
-1 instead.
Constraints
Input Specification
The first line contains two space-separated integers, and
.
Output Specification
If there does not exist a sequence of operations which makes equal to
, output
-1 on one line.
Otherwise, on the first line, output the minimum length of a valid operation sequence. On the second line, output a string consisting only of the characters +, *, and ^, describing any minimum length sequence of operations which makes equal to
.
Sample Input
2 3
Sample Output
5
+^+^*
Explanation for Sample
The values which takes on during this sequence of operations are as follows:
Note that +^+*^ would also be an acceptable operation sequence.
It can be shown that the minimal number of operations to transform from
to
is
.
Comments