class monome:
    def __init__(self,coef,deg):
        self.coef = coef 
        self.deg = deg 

    def affiche(self):
        aff = ''
        if self.deg == 0 and abs(self.coef) != 1:
            X = ''
        elif self.deg == 0:
            X = '1'
        elif self.deg == 1:
            X = 'X'
        else:
            X = 'X^' + str(self.deg)
            
        if self.coef < 0 and self.coef != -1:
            aff = str(self.coef) + X
        elif self.coef == -1:
            aff = '-' + X
        elif self.coef > 0 and self.coef != 1:
            aff = '+' + str(self.coef) + X 
        elif self.coef == 1:
            aff = '+' + X   
            
        return aff

    def __add__(self , other):
        if self.deg != other.deg: return False
        else: return monome(self.coef + other.coef , self.deg)

    def __sub__(self , other):
        if self.deg != other.deg: return False
        else: return monome(self.coef - other.coef , self.deg)

    def __mul__(self , other):
        return monome(self.coef * other.coef , self.deg + other.deg)

class polynome:
    def __init__(self,*args):
        self.ListeMonomes = [ monome( args[d] , d ) for d in range(len(args)) ]

    def affiche(self):
        r , prems = '' , True
        for m in self.ListeMonomes:
            r += m.affiche()
            if prems == True and m.coef > 0:
                r = r[1:]
            prems = False
        return r

    def standardise(self , other):
        if len(self.ListeMonomes) != len(other.ListeMonomes):
            if len(self.ListeMonomes) == max( len(self.ListeMonomes) , len(other.ListeMonomes) ): # si deg(other) < deg(self)
                for i in range(len(self.ListeMonomes)-len(other.ListeMonomes)):
                    other.ListeMonomes += [ monome(0 , len(other.ListeMonomes) + i) ]
            else:
                for i in range(len(other.ListeMonomes)-len(self.ListeMonomes)):
                    self.ListeMonomes += [ monome(0 , len(self.ListeMonomes) + i) ]
        
        P = [m.coef for m in self.ListeMonomes]
        Q = [m.coef for m in other.ListeMonomes]
        
        return polynome(*P) , polynome(*Q)

    def __add__(self , other):
        P , Q = self.standardise(other)
        R = []
        for d in range( len(P.ListeMonomes) ):
            R += [ P.ListeMonomes[d] + Q.ListeMonomes[d] ]
        
        return polynome(*[ m.coef for m in R ])

    def __sub__(self , other):
        P , Q = self.standardise(other)
        R = []
        for d in range( len(P.ListeMonomes) ):
            R += [ P.ListeMonomes[d] - Q.ListeMonomes[d] ]
        
        return polynome(*[ m.coef for m in R ])

    def __mul__(self , other):
        R , R_final = [] , []
        for m in self.ListeMonomes:
            for mm in other.ListeMonomes:
                R += [ m * mm ]
        
        for degre in range(len(self.ListeMonomes) + len(other.ListeMonomes)):
            result = monome(0,degre)
            for m in [ monome for monome in R if monome.deg == degre ]:
                result += m
            R_final.append(result)

        return polynome(*[ m.coef for m in R_final])

P, Q = polynome(1,2,3), polynome(-2,5,0,7)

A, S, M = P + Q, P - Q, P * Q

print( '(P+Q)(X)={}, (P-Q)(X)={}, (P*Q)(X)={}'.format(A.affiche(),S.affiche(),M.affiche() ) )