Negative Ones

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 1G

Author:
Problem type

You are given an irreducible fraction, \frac ab. Let the initial value of a variable x be -1. There are three available operations on x:

  1. + - Add -1 to x.
  2. * - Multiply x by -1.
  3. ^ - Raise x to the power of -1.

Note that x must be nonzero for operation type 3 ^.

Output any minimum length sequence of operations which makes x equal to \frac ab. If it is not possible, output -1 instead.

Constraints

|a| \le 10^6
1 \le b \le 10^6

\gcd(a,b) = 1

Input Specification

The first line contains two space-separated integers, a and b.

Output Specification

If there does not exist a sequence of operations which makes x equal to \frac ab, 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 x equal to \frac ab.

Sample Input

2 3

Sample Output

5
+^+^*

Explanation for Sample

The values which x takes on during this sequence of operations are as follows:

-1 \rightarrow -2 \rightarrow -\frac12 \rightarrow -\frac32 \rightarrow -\frac23 \rightarrow \frac23

Note that +^+*^ would also be an acceptable operation sequence.

It can be shown that the minimal number of operations to transform x from -1 to \frac23 is 5.


Comments

There are no comments at the moment.