#C14872. Polynomial Operations

    ID: 44569 Type: Default 1000ms 256MiB

Polynomial Operations

Polynomial Operations

You are given two polynomials P1(x) and P2(x) represented by their coefficients in increasing order of powers of x. That is, a polynomial P(x) = a0 + a1x + a2x2 + \dots + anxn is given by the list of coefficients [a0, a1, ..., an].

Your task is to implement a Polynomial class (or equivalent in your language of choice) with the following operations:

  • Addition: P(x) + Q(x)
  • Subtraction: P(x) - Q(x)
  • Multiplication: P(x) \times Q(x)
  • Evaluation: Compute P(x) at a given value of x.
  • String Representation: Convert the polynomial into a human-readable string where nonzero terms are shown in the form:
    • For the constant term: "a"
    • For x1: "ax"
    • For higher powers: "axk"
    and terms are joined with " + ". If all coefficients are zero, output "0".

The input will give you the coefficients for two polynomials and an evaluation point. You are required to:

  1. Construct the two polynomials P1 and P2 from the given coefficients.
  2. Compute P1 + P2, P1 - P2, and P1 \times P2.
  3. Evaluate both P1 and P2 at the given point.
  4. Print the string representations of P1, P2, their sum, difference, product, and the evaluations.
  5. </p>

    Note: When displaying formulas, use LaTeX for mathematical notation. For example, the polynomial should be represented as \(a_0 + a_1x + a_2x^2 + \dots + a_nx^n\).

    inputFormat

    The input is read from standard input (stdin) and has the following format:

    n1
    c11 c12 ... c1n1
    n2
    c21 c22 ... c2n2
    x
    

    Where:

    • n1 is the number of coefficients of the first polynomial P1.
    • The next line contains n1 integers representing the coefficients of P1 in increasing order of powers.
    • n2 is the number of coefficients of the second polynomial P2.
    • The following line contains n2 integers for P2.
    • x is the evaluation point.

    outputFormat

    Print the following information to standard output (stdout), each on a new line:

    p1: {P1_string}
    p2: {P2_string}
    p1 + p2: {sum_string}
    p1 - p2: {diff_string}
    p1 * p2: {prod_string}
    p1 evaluated at x={x}: {P1_value}
    p2 evaluated at x={x}: {P2_value}
    

    Where {P1_string}, {P2_string}, etc. are the string representations and computed values of the respective polynomials.

    ## sample
    3
    1 2 3
    3
    0 -1 4
    2
    
    p1: 1 + 2x + 3x^2
    

    p2: -1x + 4x^2 p1 + p2: 1 + 1x + 7x^2 p1 - p2: 1 + 3x + -1x^2 p1 * p2: -1x + 2x^2 + 5x^3 + 12x^4 p1 evaluated at x=2: 17 p2 evaluated at x=2: 14

    </p>