from fractions import Fraction

def decomp(n):
    D = dict() # dictionnaire vide
    
    k = 2
    
    while n > 1:
        exposant = 0
        while n%k == 0:
            exposant = exposant + 1
            n = n/k
        if exposant != 0:
            D[k] = exposant
        k = k+1
        j = 2
        while k%j == 0:
            k = k + 1 
        
    return D

class Rc:
    def __init__(self,a,b):
        self.coef = Fraction(a)
        self.radicand = Fraction(b)
        
    def simp(self):
        x = self.radicand
        numer = decomp(x.numerator)
        denom = decomp(x.denominator)
        coef_num, radicand_num = 1, 1
        for facteur, exposant in numer.items():
            coef_num *= facteur**(exposant//2)
            if exposant%2 == 1:
                radicand_num *= facteur
                
        coef_denom, radicand_denom = 1, 1
        for facteur, exposant in denom.items():
            coef_denom *= facteur**(exposant//2)
            if exposant%2 == 1:
                radicand_denom *= facteur
                
        return Fraction(coef_num,coef_denom)*self.coef, Fraction(radicand_num,radicand_denom)
    
    def __str__(self):
        c, r = self.simp()[0], self.simp()[1]
        
        if r.denominator != 1:
            c, r = Fraction(c.numerator,c.denominator*r.denominator), Fraction(r.denominator,1)
        
        if r == 1:
            return c.__str__()
        elif c == 1:
            return f'√({self.simp()[1]})'
        else:
            return f'({c})√({r})'
        
    def __mul__(self, other):
        c,r = other.coef, other.radicand
        return Rc( self.coef * c , self.radicand * r)#.__str__()
        
    def __add__(self, other):
        c,r = other.coef, other.radicand
        s1, s2 = self.simp(), Rc(c,r).simp()
        if s1[1] == s2[1]:
            result = Rc(s1[0]+s2[0] , s1[1])
            return result.__str__()
        else:
            return self.__str__() + ' + ' + other.__str__()
        
    def __sub__(self, other):
        return self.__add__(Rc(-other.coef,other.radicand))
    
    def __truediv__(self, other):
        return self.__mul__( Rc(1/other.coef,1/other.radicand) )
    
    def __pow__(self,n):
        r = Rc(1,1)
        for _ in range(n):
            r *= self
            
        return r