Expression
Time limit4sMemory limit512 MB
For each pair (x, y) print a postfix expression over x, +, -, *, / whose value is y, using the fixed construction the statement describes.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Implementation, Brute force
- Solved
- No attempts yet
Problem

Here is a puzzle. Can you write a mathematical expression that evaluates to and uses nothing but common mathematical symbols and four copies of the digit ?
One answer is
Here ! is the factorial sign and % is the percent sign, so 11% = 0.11.
Four fours is an arithmetic puzzle whose goal is to produce a given number from four copies of the digit and common mathematical symbols. Some numbers are easy: . Others are not, as the expression for shows. Can you produce from four fours?
This problem is a variation of that puzzle. You are given two integers and , and you have to write an expression whose value is under these rules.
- The only constant allowed in the expression is the integer .
- The only operations allowed are addition, subtraction, multiplication and division.
- The expression uses at most operations.
- No intermediate result exceeds in absolute value.
- Every division is exact, so the divisor is not zero and the quotient is an integer.
Many expressions can satisfy these rules at once. The output section fixes one of them, and that is the expression you print.
Input
The first line contains an integer , the number of test cases.
Each of the next lines contains two integers and separated by a space.
Output
For each test case print one line.
An expression is written in postfix notation as a string over the five characters x, +, -, * and /. It is evaluated with a stack that starts empty, reading the string from left to right.
xpushes the number onto the stack.+pops two numbers and in that order, then pushes .-pops and , then pushes .*pops and , then pushes ./pops and , then pushes .
After the last character the stack must hold exactly one number, and that number is the value of the expression. If an operator is read while the stack holds fewer than two numbers, or the stack does not hold exactly one number at the end, the evaluation fails.
Print the expression that this rule selects.
- If and , print
IMPOSSIBLE. - If , print
xx-. - Otherwise print followed by
x/.
is defined for and is a postfix string whose value is . Let be the number of x characters in . The base string for is x, so . Three rules build the other strings.
- Product: if with , concatenate , then , then
*x/. The result holds charactersx. - Sum: if with , concatenate , then , then
+. The result holds charactersx. - Difference: if with , concatenate , then , then
-. The result holds charactersx.
is the smallest number of x characters over every string these rules build for , and is a string that reaches . When several strings reach it, take a product first, then a sum, then a difference, and inside one kind take the smallest . The two parts of such a string always satisfy and , so is determined for every .
The expression printed this way uses at most operations.