eolymp
bolt
Try our new interface for solving problems
Problems

Symbolic Mathematics

Symbolic Mathematics

Over the years software systems that can perform symbolic computation have improved considerably. Popular commercial systems currently include Mathematica and Maple; the Sage system is also freely available. In this problem you're just going to implement a small part of such a system: the ability to compute (symbolically) \textbf{f(g(x))} for the functions \textbf{f(x)} and \textbf{g(x)} expressed as polynomials with terms having non-negative integer exponents and integer coefficients. \InputFile Polynomials will be expressed in the input and output as a sequence of terms followed immediately by the end of line. Each term consists of a plus or minus sign (except the first term doesn’t have a sign if it is plus), an integer coefficient (omitted if it is \textbf{1} and the term's exponent is non-zero), the lowercase letter '\textbf{x}', a circumflex ('\textbf{^}'), and a non-negative exponent. If the exponent is \textbf{1}, then the circumflex and the exponent are omitted. If the exponent is\textbf{0}, the '\textbf{x}' is also omitted. Terms are given in decreasing order of their exponents, and each term must have a unique exponent. The table below shows a few example polynomials in this form along with their traditional algebraic representation. The input will contain pairs of lines. The first line of each pair gives the polynomial \textbf{f(x)}, and the second line of each pair gives the polynomial \textbf{g(x)}. The input line after the last pair of polynomials will contain only an end of line character. Blanks and tabs will never appear in the input. Coefficients and exponents in the input and output polynomials will never exceed \textbf{1000000} in absolute value. \OutputFile For each pair of input lines, display the input case number (\textbf{1}, \textbf{2}, …), the input polynomials, and the polynomial representing the value of \textbf{f(g(x))}. Label the polynomials appropriately. Display a blank line after the output for each case. Follow the format shown in the samples below.
Time limit 1 second
Memory limit 64 MiB
Input example #1
x+1
4
2x^2-x+5
-3x^3+x

Output example #1
Case 1:
  f(x) = x+1
  g(x) = 4
  f(g(x)) = 5

Case 2:
  f(x) = 2x^2-x+5
  g(x) = -3x^3+x
  f(g(x)) = 18x^6-12x^4+3x^3+2x^2-x+5

Source ACM North Central North America Regional Programming Contest, November 12, 2011