QOJ.ac

QOJ

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

#7811. Quadratic Equation

統計

As is well known, for a quadratic equation $ax^2 + bx + c = 0, (a \neq 0)$, real solutions can be found as follows: Calculate $\Delta = b^2 - 4ac$, then: 1. If $\Delta < 0$, the quadratic equation has no real solutions. 2. Otherwise, $\Delta \geq 0$, in which case the quadratic equation has two real solutions $x_{1,2} = \frac{-b \pm \sqrt{\Delta}}{2a}$. Here, $\sqrt{\Delta}$ denotes the arithmetic square root of $\Delta$, which is the unique non-negative real number $s$ such that $s^2 = \Delta$. * Specifically, when $\Delta = 0$, these two real solutions are equal; when $\Delta > 0$, these two real solutions are distinct.

For example: $x^2 + x + 1 = 0$ has no real solutions because $\Delta = 1^2 - 4 \times 1 \times 1 = -3 < 0$. $x^2 - 2x + 1 = 0$ has two equal real solutions $x_{1,2} = 1$. * $x^2 - 3x + 2 = 0$ has two distinct real solutions $x_1 = 1, x_2 = 2$.

In this problem, the greatest common divisor of $a$ and $b$ is denoted as $\gcd(a, b)$. For example, the greatest common divisor of 12 and 18 is 6, i.e., $\gcd(12, 18) = 6$.

Description

Given the coefficients $a, b, c$ of a quadratic equation, where $a, b, c$ are integers and $a \neq 0$, you need to determine whether the quadratic equation $ax^2 + bx + c = 0$ has real solutions and output them in the required format.

When outputting a rational number $v$ in this problem, you must follow these rules: By the definition of rational numbers, there exist unique integers $p$ and $q$ such that $q > 0$, $\gcd(p, q) = 1$, and $v = \frac{p}{q}$. If $q = 1$, output $\{p\}$; otherwise, output $\{p\}/\{q\}$; where $\{n\}$ represents the value of the integer $n$. For example: When $v = -0.5$, the values of $p$ and $q$ are $-1$ and $2$, so output -1/2. * When $v = 0$, the values of $p$ and $q$ are $0$ and $1$, so output 0.

For solving the equation, consider two cases: 1. If $\Delta = b^2 - 4ac < 0$, the equation has no real solutions, and you should output NO. 2. Otherwise, $\Delta \geq 0$, in which case the equation has two solutions (possibly equal). Let $x$ be the larger of the two. Then: (1). If $x$ is a rational number, output $x$ in the rational number format. (2). Otherwise, based on the formula above, $x$ can be uniquely represented as $x = q_1 + q_2\sqrt{r}$, where: $q_1, q_2$ are rational numbers, and $q_2 > 0$. $r$ is a positive integer, $r > 1$, and there is no positive integer $d > 1$ such that $d^2 \mid r$ (i.e., $r$ should not be a multiple of $d^2$).

At this point: 1. If $q_1 \neq 0$, output $q_1$ in the rational number format, followed by a plus sign +. 2. Otherwise, skip this step.

Subsequently: 1. If $q_2 = 1$, output sqrt({r}). 2. Otherwise, if $q_2$ is an integer, output {q_2}*sqrt({r}). 3. Otherwise, if $q_3 = \frac{1}{q_2}$ is an integer, output sqrt({r})/{q_3}. 4. Otherwise, it can be proven that there exist unique integers $c, d$ such that $c, d > 1, \gcd(c, d) = 1$ and $q_2 = \frac{c}{d}$, in which case output {c}*sqrt({r})/{d}.

In the representations above, $\{n\}$ represents the value of the integer $n$. See the examples for details.

If the equation has real solutions, output the larger of the two real solutions in the required format. Otherwise, if the equation has no real solutions, output NO.

Input

The first line contains two positive integers $T$ and $M$, representing the number of equations and the upper bound of the absolute values of the coefficients, respectively. The next $T$ lines each contain three integers $a, b, c$.

Output

Output $T$ lines, each containing a string representing the answer to the corresponding query, formatted as described. The string in each line should not contain any spaces.

Examples

Input 1

9 1000
1 -1 0
-1 -1 -1
1 -2 1
1 5 4
4 4 1
1 0 -432
1 -3 1
2 -4 1
1 7 1

Output 1

1
NO
1
-1
-1/2
12*sqrt(3)
3/2+sqrt(5)/2
1+sqrt(2)/2
-7/2+3*sqrt(5)/2

Examples 2

See uqe/uqe2.in and uqe/uqe2.ans in the contestant's directory.

Constraints

For all test data: $1 \leq T \leq 5000$, $1 \leq M \leq 10^3$, $|a|, |b|, |c| \leq M$, $a \neq 0$.

Test Case ID $M \leq$ Special Property A Special Property B Special Property C
1 1 Yes Yes Yes
2 20 No No No
3 $10^3$ Yes No Yes
4 $10^3$ Yes No No
5 $10^3$ No Yes Yes
6 $10^3$ No Yes No
7, 8 $10^3$ No No Yes
9, 10 $10^3$ No No No

Where: Special Property A: Guaranteed $b = 0$. Special Property B: Guaranteed $c = 0$. * Special Property C: If the equation has solutions, both solutions are integers.

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.