QOJ.ac

QOJ

実行時間制限: 6 s メモリ制限: 512 MB 満点: 100

#5294. Factorization

統計

By the Fundamental Theorem of Algebra, we know that if we count roots with multiplicity, an $n$-th degree polynomial has exactly $n$ zeros (points where the function value is $0$) in the complex field. Given an integer-coefficient polynomial $F[x]$, all of its $n$ zeros are rational numbers (i.e., they can be written as the quotient of two integers). Furthermore, if we take all its non-zero zeros (where the function argument is not $0$ and the function value is $0$) and remove duplicates, we obtain $r$ distinct non-zero zeros, where the $i$-th non-zero zero can be expressed as:

$$\mathrm{sgn}_i \times \frac{q_i}{p_i}$$

In this expression, $\mathrm{sgn}_i$ represents the sign of the $i$-th zero, and $p_i$ and $q_i$ are two coprime positive integers.

Given $F[x]$, you are required to output its factored form.

Input

The input consists of a single line containing the polynomial $F[x]$.

The polynomial is guaranteed to be in the following form:

  • $a_n x^n + a_{n-1}x^{n - 1} + \dots + a_1x + a_0$

The terms are ordered from highest degree to lowest. $a_i$ are integers. If $a_i$ is $0$, the term is omitted. If $a_i$ is negative, the preceding plus sign is omitted. If the absolute value of $a_i$ is $1$ and $i \neq 0$, the $1$ is not printed. It is guaranteed that $a_n \neq 0$.

See the sample input for details.

Output

Output a single line representing the factored form, in the following format:

  • $a_n (x + u_1/v_1)^{t_1}(x + u_2/v_2)^{t_2} \dots (x + u_s/v_s)^{t_s}$

Where $u$ and $v$ are coprime, and $v$ is a positive integer.

The terms $u_i/v_i$ are arranged in descending order. If $u_i/v_i = 0$, the term is $x^{t_i}$. If $u_i/v_i$ is negative, the plus sign is omitted. If $v_i$ is $1$, the $/v_i$ is omitted.

If $t_i$ is $1$, the exponent $^{t_i}$ is omitted.

If $a_n$ is $\pm 1$, the $1$ is omitted.

See the sample output for details.

Examples

Input 1

8x^7-258x^5+2112x^3-512x

Output 1

8(x-4)^2(x-1/2)x(x+1/2)(x+4)^2

Input 2

-x^2+2x-1

Output 2

-(x-1)^2

Note

Test Case ID Maximum Degree Number of Distinct Zeros Coefficient Range (Absolute Value)
$1$ $2$ $2$ $\le 10$
$2$ $4$ $4$ $\le 100$
$3$ $7$ $7$ $\le 10^6$
$4$ $10$ $10$ $\le 10^7$
$5$ $12$ $12$ $\le 10^{16}$
$6$ $35$ $5$ $\le 10^{24}$
$7$ $39$ $5$ $\le 10^{68}$
$8$ $46$ $4$ $\le 10^{104}$
$9$ $80$ $2$ $\le 10^{12}$
$10$ $50$ $1$ $\le 10^{316}$

$p_i, q_i$ satisfy:

$$\prod_{i=1}^r p_i \le 10^6, \prod_{i=1}^r q_i \le 10^6$$

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.