#P9750. Real Root of Quadratic Equation
Real Root of Quadratic Equation
Real Root of Quadratic Equation
Given three integers (a), (b), and (c) with (a \neq 0), consider the quadratic equation [ a x^2 + b x + c = 0, ]
Your task is to determine whether the equation has real solutions or not. If it does, output the larger root (possibly repeated) in the following format:
-
When outputting a rational number (v), it must be represented in its unique reduced form as follows. There exist unique integers (p) and (q) with (q > 0) and (\gcd(p,q)=1) such that (v=\frac{p}{q}). If (q=1) then output just the integer value, i.e. simply output (p); otherwise, output it as (p/q).
For example:
- If \(v = -0.5\), then \(p=-1\) and \(q=2\) so you should output
-1/2
. - If \(v = 0\), then \(p=0\) and \(q=1\) so you should output
0
.
- If \(v = -0.5\), then \(p=-1\) and \(q=2\) so you should output
-
For solving the quadratic equation, consider the discriminant (\Delta = b^2 - 4ac):
- If \(\Delta < 0\), the equation has no real solutions; output
NO
. - If \(\Delta \ge 0\), let \(x\) be the larger of the two solutions. Then:
- If \(x\) is rational, output it in the rational format described above.
- If \(x\) is irrational, it can be uniquely represented as \[
x = q_1 + q_2 \sqrt{r}, ] where:
- (q_1) and (q_2) are rational numbers with (q_2 > 0);
- (r) is a positive integer greater than 1 and is square-free (i.e. there is no integer (d>1) such that (d^2 \mid r)).
- If (q_1 \neq 0), first output (q_1) (in the rational format) followed by a plus sign
+
; - Then, for the (\sqrt{r})-part, do the following:
- If (q_2 = 1), output
sqrt({r})
; - If (q_2) is an integer (and not 1), output
{q2}*sqrt({r})
; - If the reciprocal (q_3 = 1/q_2) is an integer, output
sqrt({r})/{q3}
; - Otherwise, if (q_2 = c/d) in lowest terms (with (c, d > 1) and (\gcd(c,d)=1)), output
{c}*sqrt({r})/{d}
.
- If (q_2 = 1), output
</p>
In summary, if the quadratic equation has a real solution, output the larger one in the correct format as illustrated above; otherwise, output
NO
.inputFormat
The input consists of three space-separated integers (a), (b), and (c) (with (a \neq 0)).
outputFormat
Output the larger real root in the required format if it exists; otherwise, output
NO
.sample
1 -3 2
2
- If \(\Delta < 0\), the equation has no real solutions; output