This page is still under construction.

Parts of this page are still being built. What you see may change.

Exact Arithmetic

Time limit1sMemory limit512 MB

Summary
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 XX be the set of all rational numbers and all numbers of the form qrq \sqrt{r}, where qq is a non-zero rational number and rr is an integer greater than 11. Here, rr must not have a square number other than 11 as a divisor. Also, let X∗X^{*} be the set of all numbers that can be expressed as a sum of one or more elements of XX.

Consider the machine YY, a stack-based calculator that operates on values in X∗X^{*} and has the instructions shown below.

  • push nn: pushes the integer specified in the operand onto the stack.
  • add: pops two values x1x_1 and x2x_2 from the top of the stack in this order, then pushes (x2+x1)(x_2 + x_1).
  • sub: pops two values x1x_1 and x2x_2 from the top of the stack in this order, then pushes (x2−x1)(x_2 - x_1).
  • mul: pops two values x1x_1 and x2x_2 from the top of the stack in this order, then pushes (x2⋅x1)(x_2 \cdot x_1).
  • div: pops two values x1x_1 and x2x_2 from the top of the stack in this order, then pushes (x2/x1)(x_2 / x_1). Here, x1x_1 must be a non-zero value in XX (not in X∗X^{*}).
  • sqrt: pops one value xx from the stack and pushes the square root of xx. Here, xx must be a non-negative rational number.
  • disp: pops one value xx from the stack and outputs the string representation of xx 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 YY, no more values can be pushed when the stack already holds 256256 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 32 76832\,768 in absolute value.
  • For any element of XX of the form qr=(a/b)rq \sqrt{r} = (a / b) \sqrt{r}, both ∣ar∣≤32 768|a \sqrt{r}| \le 32\,768 and ∣b∣≤32 768|b| \le 32\,768 must hold.

For any element of X∗X^{*}, each term in the sum must satisfy the conditions above.

The rules for the string representations of values on machine YY are as follows:

  • A rational number is represented either as an integer or as an irreducible fraction whose denominator is greater than 11.
  • A fraction is represented as num\mathit{num}/den\mathit{den}, where num\mathit{num} is the numerator and den\mathit{den} is the denominator. The sign character - precedes negative numbers.
  • A number of the form qrq \sqrt{r} is represented as rep\mathit{rep}*sqrt(rr), except when ∣q∣=1|q| = 1; in that case it is represented as sqrt(rr) for q=1q = 1 or -sqrt(rr) for q=−1q = -1. Here, rep\mathit{rep} is the string representation of qq.
  • A sum of two or more elements of XX 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 1\sqrt{1}. 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 YY.

Input

The input is a sequence of no more than 30003000 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 YY displays. Write each string on a separate line.

Examples1

  1. Example 1

    Input
    push 1
    push 2
    sqrt
    div
    push 1
    push 3
    sqrt
    div
    add
    disp
    push 8
    sqrt
    push 3
    push 2
    sqrt
    mul
    add
    disp
    stop
    
    Expected output
    1/2*sqrt(2) + 1/3*sqrt(3)
    5*sqrt(2)