Polynomial Showdown

Time limit1sMemory limit128 MB

Summary
Given nine polynomial coefficients from degree 8 down to the constant, print each polynomial with signs, exponents, and coefficients formatted to drop all unnecessary characters.
Level

Easy3 of 10

Topics
Implementation, Simulation, Math, String
Solved
No attempts yet

Problem

You are given the coefficients of a polynomial, listed from degree 8 down to degree 0 (the constant term). Print the polynomial in a clean, human-readable form with every unnecessary character removed.

For example, from the coefficients 0, 0, 0, 1, 22, -333, 0, 1, -1 you must print x^5 + 22x^4 - 333x^3 + x - 1.

The formatting rules are:

  1. Terms appear in decreasing order of degree.
  2. An exponent is written after a caret ^.
  3. The constant term is written as the number alone.
  4. Only terms with a nonzero coefficient are printed. If every coefficient is zero, print the single constant term 0.
  5. The only spaces are a single space on each side of the binary + and - operators.
  6. If the leading term is positive, no sign precedes it; a negative leading term is preceded by a minus sign, as in -7x^2 + 30x + 66.
  7. A negated term is printed as the subtraction of its unnegated value (except for a negative leading term, which follows rule 6). So instead of x^2 + -3x, print x^2 - 3x.
  8. The coefficients 1 and -1 are shown as a number only when they are the constant term. So instead of -1x^3 + 1x^2 + 3x^1 - 1, print -x^3 + x^2 + 3x - 1.

Input

The input contains one or more lines. Each line holds nine coefficients separated by one or more spaces, listed from the degree-8 coefficient down to the degree-0 (constant) coefficient. Every coefficient is an integer whose absolute value is less than 1000.

Output

For each input line, print the corresponding formatted polynomial on its own line, following the rules above.

Examples1

  1. Example 1

    Input
    0 0 0 1 22 -333 0 1 -1
    0 0 0 0 0 0 -55 5 0
    
    Expected output
    x^5 + 22x^4 - 333x^3 + x - 1
    -55x^2 + 5x