Exact Arithmetic
Time limit1sMemory limit512 MB
Simulate a stack calculator whose values are sums of rationals and rational multiples of square roots, and print each result in a canonical exact form.
- Level
Hard8 of 10
- Topics
- Implementation, Math, Number theory, Simulation
- Solved
- No attempts yet
Problem
Let be the set of all rational numbers and all numbers of the form , where is a non-zero rational number and is an integer greater than . Here, must not have a square number other than as a divisor. Also, let be the set of all numbers that can be expressed as a sum of one or more elements of .
Consider the machine , a stack-based calculator that operates on values in and has the instructions shown below.
push: pushes the integer specified in the operand onto the stack.add: pops two values and from the top of the stack in this order, then pushes .sub: pops two values and from the top of the stack in this order, then pushes .mul: pops two values and from the top of the stack in this order, then pushes .div: pops two values and from the top of the stack in this order, then pushes . Here, must be a non-zero value in (not in ).sqrt: pops one value from the stack and pushes the square root of . Here, must be a non-negative rational number.disp: pops one value from the stack and outputs the string representation of to the display. The representation rules are stated below.stop: terminates the calculation. The stack must be empty when this instruction is called.
Initially, the stack is empty. A sufficient number of values must be present on the stack when every instruction is executed. In addition, because of the limits of machine , no more values can be pushed when the stack already holds values. There are also several restrictions on the values pushed onto the stack:
- For rational numbers, neither the numerator nor the denominator in lowest terms may exceed in absolute value.
- For any element of of the form , both and must hold.
For any element of , each term in the sum must satisfy the conditions above.
The rules for the string representations of values on machine are as follows:
- A rational number is represented either as an integer or as an irreducible fraction whose denominator is greater than .
- A fraction is represented as
/, where is the numerator and is the denominator. The sign character-precedes negative numbers. - A number of the form is represented as
*sqrt(), except when ; in that case it is represented assqrt()for or-sqrt()for . Here, is the string representation of . - A sum of two or more elements of is represented by joining the string representations of all its non-zero elements with the binary operator
+. All terms with the same rooted number are merged into a single term, and the terms must appear in ascending order of their root component. For this rule, every rational number is regarded as carrying . There is exactly one space character before and after each binary operator+. No space characters appear anywhere else.
The following are a few examples of valid string representations:
0
1
-1/10
2*sqrt(2) + 1/2*sqrt(3) + -1/2*sqrt(5)
1/2 + sqrt(10) + -sqrt(30)
Your task is to write a program that simulates machine .
Input
The input is a sequence of no more than instructions. Each line contains a single instruction. You may assume that every instruction is called in a legal way. The instruction "stop" appears only once, at the end of the entire input.
Output
Output the strings that machine displays. Write each string on a separate line.