Digit Short Coding
Time limit1sMemory limit1024 MB
Given N up to 10^8, find the shortest postfix expression of digits, * and ^ that evaluates to N.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, Number theory
- Solved
- No attempts yet
Problem
A positive integer is given. Find the shortest expression that represents using the digits 0 through 9 and the operators multiplication * and exponentiation ^.
Write the expression in postfix notation, and separate each number or operator with a space. Spaces do not count toward the length of the expression.
Input
A positive integer is given.
Output
Print the shortest expression representing in postfix notation. If several shortest expressions exist, print any one of them.
Hint
Postfix notation writes each operator after its operands. Reading the expression from left to right, push numbers onto a stack. When an operator appears, pop two numbers from the stack, compute the result, and push it back.
For example, the postfix expression 3 2 3 * ^ evaluates to as follows.
- , , and are pushed onto the stack in order.
- Pop and , compute their product , and push it onto the stack.
- Pop and , compute the power , and push it onto the stack.