13481 - Polynomial Operations   

Description

In this partial-judged problem, you are to implement a class polynomial which supports the following basic operation:

  • Read coefficients from an istream (for instance, cin is an object of istream)
  • Write the polynomial in a readable format to an ostream (for instance, cout is an object of ostream)
  • Add two polynomials
  • Subtract two polynomials

A polynomial has two properties: an integer degree and an array coefficients. For the purpose of simplify the constructor and destructor, we use std::array<> which appeared since C++11 rather than the conventional C-style array. You can use it as is.

Due to it is a bit tricky to overload the operator>> with istream, it had been almost done for you and you only need to implement a method to complete the operator.

Check function.h cautiously before you start.

Input

There are several testcases, each of which contains two lines of polynomials \(A(x), B(x)\) and a black line. For each line of polynomial, it is started by an interger \(d\) indicating the degree of the polynomial, followed by \(d+1\) intergers which are the coeffcients in the descending order.

The degree won't exceed \(10^5\). (It's defined as N in function.h.)

It's guranteed that the polynomial is valid, i.e., the leading coefficient is not zero.

For 20% of testcases, the coefficients of \(A(x), B(x), A(x)-B(x)\) are positive.

For 50% of testcases, the coefficients of \(A(x), B(x)\) are positive and the coefficient of \(A(x)-B(x)\) are non-negative.

Output

For every two polynomial, the main() would print out \(A(x), B(x), A(x)+B(x), A(x)-B(x)\) and a blank line.

The output format of the polynomial is:

  • A polynomial consists of several terms. Print them in descending order.
  • A term has 3 properties: sign, coefficient (so it is always positive) and exponent.
    • For the sign:
      • If it is leading term, then print the minus sign when the leading coefficient is negative, else not print any if it is positive.
      • Otherwise print the sign with spaces around it. (" + " or " - " without the quotation marks)
    • For the coefficient \(c\), print \(c\) if \(c\ne1\) or it is a constant term.
    • For the exponent \(e\), print x^\(e\) if \(e>1\), else print x if \(e=1\), otherwise not print any if it is a constant term.
  • Only term whose coefficient is not zero should be printed, except that the polynomial is a zero polynomial, i.e. degree is zero and constant term is also zero.

The sample output includes many examples.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13481.cpp

Partial Judge Header

13481.h

Tags




Discuss