import java.io.*; import java.util.*; public class ExactArithmetic { //---------------------------------------------------------------------- private static Scanner cin = new Scanner(System.in); //---------------------------------------------------------------------- private static class FracSqrt { public final int a; public final int b; public final int r; public static final FracSqrt ZERO = new FracSqrt(0, 1, 1); public FracSqrt(int a, int b, int r) { if(b == 0) { throw new IllegalArgumentException("b"); } if(r <= 0) { throw new IllegalArgumentException("r"); } if(a == 0) r = 1; for(int i = 2; i * i <= r; i++) while(r % (i * i) == 0) { r /= i * i; a *= i; } int x = Math.abs(a); int y = Math.abs(b); { int z; while((z = x % y) != 0) { x = y; y = z; } } if(b < 0) { y = -y; } this.a = a / y; this.b = b / y; this.r = r; } public FracSqrt add(FracSqrt that) { if(this.r != that.r) { throw new IllegalArgumentException("that"); } int a = this.a * that.b + that.a * this.b; int b = this.b * that.b; return new FracSqrt(a, b, r); } public FracSqrt mul(FracSqrt that) { return new FracSqrt(a * that.a, b * that.b, r * that.r); } public FracSqrt neg() { return new FracSqrt(-a, b, r); } public FracSqrt inv() { return new FracSqrt(b, a * r, r); } public String toString() { if(r == 1) { if(b == 1) return Integer.toString(a); return a + "/" + b; } String s = "sqrt(" + r + ")"; if(b != 1) return a + "/" + b + "*" + s; if(a == +1) return s; if(a == -1) return "-" + s; return a + "*" + s; } } //---------------------------------------------------------------------- public static class Value { private Map map; private Value() { map = new TreeMap(); } private void acc(FracSqrt val) { int r = val.r; // prohibits zero if(map.containsKey(r)) { val = map.get(r).add(val); if(val.a != 0) map.put(r, val); else map.remove(r); } else { if(val.a != 0) map.put(r, val); } } public Value add(Value val) { Value x = new Value(); x.map.putAll(map); for(int r : val.map.keySet()) { x.acc( val.map.get(r) ); } return x; } public Value sub(Value val) { Value x = new Value(); x.map.putAll(map); for(int r : val.map.keySet()) { x.acc( val.map.get(r).neg() ); } return x; } public Value mul(Value val) { Value x = new Value(); for(FracSqrt v1 : map.values()) for(FracSqrt v2 : val.map.values()) x.acc( v1.mul(v2) ); return x; } public Value div(Value val) { if(val.map.size() != 1) { throw new IllegalArgumentException("val"); } FracSqrt v2 = val.map.values().iterator().next().inv(); Value x = new Value(); for(FracSqrt v1 : map.values()) x.acc( v1.mul(v2) ); return x; } public Value sqrt() { Value x = new Value(); if(map.isEmpty()) return x; if(map.size() > 1) throw new IllegalStateException(); if(! map.containsKey(1) ) throw new IllegalStateException(); FracSqrt v1 = map.get(1); x.acc(new FracSqrt(1, v1.b, v1.a * v1.b)); return x; } public static Value valueOf(int val) { Value x = new Value(); if(val != 0) x.map.put(1, new FracSqrt(val, 1, 1)); return x; } public String toString() { if(map.isEmpty()) return "0"; StringBuffer sb = new StringBuffer(); String op = ""; for(FracSqrt v1 : map.values()) { sb.append(op); sb.append(v1); op = " + "; } return sb.toString(); } } //---------------------------------------------------------------------- public static void main(String[] args) { Stack stack = new Stack(); while(true) { String s = cin.next(); if(s.equals("push")) { stack.push(Value.valueOf(cin.nextInt())); } else if(s.equals("add")) { Value x1 = stack.pop(); Value x2 = stack.pop(); stack.push(x2.add(x1)); } else if(s.equals("sub")) { Value x1 = stack.pop(); Value x2 = stack.pop(); stack.push(x2.sub(x1)); } else if(s.equals("mul")) { Value x1 = stack.pop(); Value x2 = stack.pop(); stack.push(x2.mul(x1)); } else if(s.equals("div")) { Value x1 = stack.pop(); Value x2 = stack.pop(); stack.push(x2.div(x1)); } else if(s.equals("sqrt")) { stack.push(stack.pop().sqrt()); } else if(s.equals("disp")) { System.out.println(stack.pop().toString()); } else if(s.equals("stop")) { break; } } if(!stack.empty()) { throw new IllegalStateException(); } } //---------------------------------------------------------------------- }