#P1067. [NOIP 2009 普及组] 多项式输出

[NOIP 2009 普及组] 多项式输出

Description

A univariate polynomial of degree nn can be written as:

$$f(x)=a_nx^n+a_{n-1}x^{n-1}+\cdots +a_1x+a_0,a_n\ne 0$$

Here, aixia_ix^i is called the ii-th degree term, and aia_i is its coefficient. Given the degree and coefficients of a univariate polynomial, output the polynomial in the following format:

  1. The indeterminate is xx. List the terms from left to right in descending order of degree.
  2. Include only terms whose coefficients are nonzero.
  3. If the leading (degree-nn) coefficient is positive, the polynomial must not start with a + sign; if it is negative, the polynomial must start with a - sign.
  4. For any non-leading term, connect it to the previous term with + or -, indicating a positive or negative coefficient, respectively. Immediately follow with the absolute value of the coefficient as a positive integer (for terms of degree greater than 00, omit the 11 if the coefficient’s absolute value is 11). If the exponent of xx is greater than 11, write it as “xbx^b”, where bb is the exponent; if the exponent is 11, write it as xx; if the exponent is 00, output only the coefficient.
  5. There must be no extra spaces at the beginning or the end of the polynomial.

Input Format

There are 22 lines of input.

  • The first line contains one integer nn, the degree of the polynomial.
  • The second line contains n+1n+1 integers. The ii-th integer is the coefficient of the term of degree ni+1n-i+1. Integers are separated by single spaces.

Output Format

Output one line: the polynomial formatted as described above.

5 
100 -1 1 -3 0 10
100x^5-x^4+x^3-3x^2+10
3 
-50 0 0 1 

-50x^3+1 

Hint

NOIP 2009 Junior, Problem 1.

Constraints: For 100% of the testdata, 0n1000 \le n \le 100, and 100ai100-100 \le a_i \le 100.

upd 2022.8.1\text{upd 2022.8.1}: A new set of hack testdata has been added.

Translated by ChatGPT 5