#P2553. Expand and Rewrite Polynomial Expression
Expand and Rewrite Polynomial Expression
Expand and Rewrite Polynomial Expression
Given an algebraic polynomial expression containing a single multiplication operation between two polynomials in the variable \(a\), expand the product and output the resulting polynomial without the multiplication operator. Each input polynomial is written in the form XXXa^YYY+XXXa^YYY+...+XXX
where:
- The only variable is \(a\).
- All terms with \(a\) are written in the form \(xa^y\) (even if \(x=1\)) and if \(y=1\) the exponent must still be explicitly written as \(a^1\).
- The constant term is written as an integer without the variable.
- Except for the first term, each subsequent term has an exponent that is exactly one less than the previous term.
Each test case is guaranteed to have exactly one occurrence of a product in the format (...)*(...)
, where each (...)
is a valid polynomial that does not include any multiplication operators.
Your task is to expand the multiplication and output the resulting polynomial expression in the same format with terms arranged in descending order of exponents. For terms with an exponent greater than 0, output them as coefficienta^exponent
; for the constant term (exponent 0), output only the coefficient.
inputFormat
The input consists of a single line containing a multiplication expression of two polynomials enclosed in parentheses. The format is:
(poly1)*(poly2)
Each polynomial is given in the form XXXa^YYY+XXXa^YYY+...+XXX
as described above.
outputFormat
Output the expanded polynomial expression (without the '*' operator) in a single line. The resulting polynomial should have its terms arranged in descending order of exponents. For terms with \(a\), output as coefficienta^exponent
(always showing the coefficient and exponent explicitly), and for the constant term (exponent 0), output the integer coefficient only. Terms are separated by a '+' symbol without extra spaces.
sample
(1a^1+1)*(1a^1+1)
1a^2+2a^1+1