#P2379. Polynomial Summation

    ID: 15651 Type: Default 1000ms 256MiB

Polynomial Summation

Polynomial Summation

You are given several polynomial expressions (monomials or polynomials) in one variable x and are required to compute their sum. Each polynomial expression consists of terms of the form ax^n (for example, 3x^2, -x, or a constant like 5). Combine like terms and output the simplified expression in descending order of powers.

More formally, given polynomial expressions \( P_1(x), P_2(x), \dots, P_n(x) \), you need to output the resulting polynomial: \[ P(x) = \sum_{i=1}^{n} P_i(x) \] with terms arranged from the highest degree to the lowest.

Notes:

  • If the coefficient of a term is 1 or -1, the coefficient should be omitted except for the constant term.
  • A term with zero coefficient should be omitted unless the resulting polynomial is zero, in which case output 0.
  • For a term with exponent 1, output x instead of x^1.

inputFormat

The first line contains an integer n (where n >= 1) representing the number of polynomial expressions. The following n lines each contain a polynomial expression. Each expression is a string composed of terms without spaces. For example: 3x^2+2x-5

outputFormat

Output a single line containing the simplified polynomial expression as described. The terms must be arranged in descending order of the exponents. If the final polynomial is 0, output 0.

sample

2
3x^2+2x-5
x^2-2x+2
4x^2-3