#P2378. Simple Quadratic Factorization
Simple Quadratic Factorization
Simple Quadratic Factorization
In this problem, you are given a quadratic polynomial in one variable (x) of the form (x^2 + bx + c) (with the coefficient of (x^2) always equal to 1). The input polynomial may be written in a simplified format where:
- The term for \(x^2\) is always present as "x^2".
- If the coefficient of \(x\) is 1 or -1, the number 1 is omitted (i.e. written as "x" or "-x").
- If either the \(x\) term or the constant term has a coefficient of 0, that term is completely omitted.
Your task is to factorize the given polynomial into two linear factors with integer constants. That is, for a polynomial
[ x^2 + bx + c, ]
find two integers (m) and (n) such that:
[ m+n = b, \quad m \times n = c. ]
Then, output the factorization in the format:
[ (x + m)(x + n), ]
where the linear factors are printed according to the following rules:
- If the constant part is 0, output the factor as simply "x".
- If the constant is positive, print it as "x+constant".
- If the constant is negative, print it as "x-abs(constant)".
It is guaranteed that the input polynomial can be factorized in this manner with integer factors. Note that the input expression will always be in a simplified format (for example, omitting the coefficient 1).
inputFormat
A single line containing a quadratic polynomial expressed in the simplified format (e.g. x^2+5x+6
, x^2-3x+2
, or x^2+4
).
outputFormat
A single line containing the factorization of the polynomial in the format (factor1)(factor2)
without any extra spaces. For example, if the input is x^2+5x+6
, the output should be (x+2)(x+3)
.
sample
x^2+5x+6
(x+2)(x+3)