P2378 Factorization II
Description
Xiaoyu has entered junior high and started learning factorization. However, she just cannot solve the problems, so she asks you for help.
She shows you her homework, which is about factorization. Since she has just started, the difficulty is lowered.
We assume the algebra in the homework uses only one variable $ x $. The highest degree is $ 2 $, and the leading coefficient is $ 1 $. When the coefficient in front of $ x $ is $ 1 $, the $ 1 $ is omitted. In short, the homework follows our usual writing style.
Let’s help her solve this “simple” problem! Although it sounds easy, programming needs to consider many cases!
It is guaranteed that all numbers in the answer are integers.
// What does this have to do with the “artifact”? God says, help others, accumulate good deeds, and the artifact will naturally appear. Hahaha, what a far-fetched explanation.
// To help AC this problem in the May monthly contest, kkksc03 secretly provides the following gentle reminder:
// The testdata may be tricky. Please consider various cases, including the case with only one input term, and whether intermediate results might overflow maxlongint, etc.
// lzn: Hey! This is leaking the problem. You will go to court for this!
// absi2011: The “//” and the characters after it in the sample output are just comments and should not be printed by your program.
/*
absi2011: The input spec is not very harmonious...
I just want to add...
If the coefficient of $ x $ or the constant term is $ 0 $, omit the entire term.
Oh, if the coefficient of $ x $ is $ -1 $, the $ 1 $ will also be omitted...
We guarantee the input can be factored, and the factorization has integer coefficients...
*/
// Original: 2022.7.24. I didn’t expect that the problem I posted in middle school has “trapped” so many people into AC-ing it. Now I’m a sophomore in college. Time flies, and I have completely forgotten how to do it, haha.
Input Format
A single polynomial, as described above. Each coefficient is an integer not greater than maxlongint.
Output Format
Output the product of factors. The answer should also follow the usual writing style. For easier grading, if the answer is written as $(x+a)(x+b)$, then $ a > b $.
What is the usual writing style?
1. $(x+0)$ is written as $ x $.
2. $(x+a)(x+a)$ is written as $(x+a)^2$.
3. Sometimes you can write $(x-3)$, but you must not write something like $(x+-3)$.
Explanation/Hint
The degree of $ x $ does not exceed $ 2 $, the number of terms does not exceed $ 3 $, and terms are in descending powers.
Translated by ChatGPT 5