n = int(input('N = '))
polinom = {}
k = 0
while k < n:
ci, pi = input().split()
ci, pi = int(ci), int(pi)
if pi in polinom:
polinom[pi] += ci
else:
polinom[pi] = ci
k += 1
s = ''
sorted_polinom = {}
for key in sorted(polinom, reverse=True):
sorted_polinom[key] = polinom[key]
if len(polinom) == 0:
pass
else:
first = True
for p in sorted_polinom:
if sorted_polinom[p] == 0:
continue
if sorted_polinom[p] < 0:
if first:
s += '-'
else:
s += ' - '
else:
if not first:
s += ' + '
first = False
if (abs(sorted_polinom[p]) == 1) and p == 0:
s += '1'
continue
if abs(sorted_polinom[p]) != 1:
s += str(abs(sorted_polinom[p]))
if p < 0:
s += 'x^({})'.format(p)
elif p == 1:
s += 'x'
elif p > 0:
s += 'x^{}'.format(p)
if len(s) == 0:
print('0')
else:
print(s)
Comments
Leave a comment