#P9750. [CSP-J 2023] 一元二次方程

[CSP-J 2023] 一元二次方程

Background

As everyone knows, for the quadratic equation in one variable ax2+bx+c=0(a0)ax ^ 2 + bx + c = 0(a \neq 0), you can find its real solutions in the following way:

  • Compute Δ=b24ac\Delta = b ^ 2 - 4ac, then:
    1. If Δ<0\Delta < 0, the quadratic equation has no real solution.
    2. Otherwise, if Δ0\Delta \geq 0, the quadratic equation has two real solutions x1,2=b±Δ2ax _ {1, 2} = \frac{-b \pm \sqrt \Delta}{2a}.

For example:

  • x2+x+1=0x ^ 2 + x + 1 = 0 has no real solution, because Δ=124×1×1=3<0\Delta = 1 ^ 2 - 4 \times 1 \times 1 = -3 < 0.
  • x22x+1=0x ^ 2 - 2x + 1 = 0 has two equal real solutions x1,2=1x _ {1, 2} = 1.
  • x23x+2=0x ^ 2 - 3x + 2 = 0 has two distinct real solutions x1=1,x2=2x _ 1 = 1, x _ 2 = 2.

In this statement, the greatest common divisor of aa and bb is denoted by gcd(a,b)\gcd(a, b). For example, the greatest common divisor of 1212 and 1818 is 66, that is, gcd(12,18)=6\gcd(12, 18) = 6.

Problem Description

Now you are given the coefficients a,b,ca, b, c of a quadratic equation in one variable, where a,b,ca, b, c are all integers and a0a \neq 0. You need to determine whether the quadratic equation ax2+bx+c=0a x ^ 2 + bx + c = 0 has real solutions, and output in the required format.

In this problem, when outputting a rational number vv, you must follow these rules:

  • By the definition of a rational number, there exist unique integers pp and qq such that q>0q > 0, gcd(p,q)=1\gcd(p, q) = 1, and v=pqv = \frac pq.
  • If q=1q = 1, output {p}; otherwise output {p}/{q}, where {n} denotes the integer value nn.
  • For example:
    • When v=0.5v = -0.5, the values of pp and qq are 1-1 and 22, so you should output -1/2.
    • When v=0v = 0, the values of pp and qq are 00 and 11, so you should output 0.

For solving the equation, discuss two cases:

  1. If Δ=b24ac<0\Delta = b ^ 2 - 4ac < 0, then the equation has no real solution, and you should output NO.

  2. Otherwise, Δ0\Delta \geq 0. The equation has two solutions (possibly equal). Let the larger one be xx, then:

    1. If xx is a rational number, output xx in the rational format.

    2. Otherwise, according to the formula above, xx can be uniquely written in the form x=q1+q2rx = q _ 1 + q _ 2 \sqrt r, where:

      • q1,q2q _ 1, q _ 2 are rational numbers, and q2>0q _ 2 > 0.
      • rr is a positive integer and r>1r > 1, and there is no positive integer d>1d > 1 such that d2rd ^ 2 \mid r (that is, rr should not be a multiple of d2d ^ 2).

    In this case:

    1. If q10q _ 1 \neq 0, output q1q _ 1 in the rational format, and then output a plus sign +.
    2. Otherwise, skip this step.

    Then:

    1. If q2=1q _ 2 = 1, output sqrt({r}).
    2. Otherwise, if q2q _ 2 is an integer, output {q2}*sqrt({r}).
    3. Otherwise, if q3=1q2q _ 3 = \frac 1{q _ 2} is an integer, output sqrt({r})/{q3}.
    4. Otherwise, it can be proven that there exist unique integers c,dc, d such that c,d>1c, d > 1, gcd(c,d)=1\gcd(c, d) = 1, and q2=cdq _ 2 = \frac cd. Output {c}*sqrt({r})/{d}.

    In the representations above, {n} denotes the integer value {n}. See the samples for details.

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

Input Format

The first line contains two positive integers T,MT, M, representing the number of equations and the upper bound of the absolute values of the coefficients.

The next TT lines each contain three integers a,b,ca, b, c.

Output Format

Output TT lines. Each line contains one string representing the answer to the corresponding query, in the format described above.

There should be no spaces in the output string of each line.

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
1
NO
1
-1
-1/2
12*sqrt(3)
3/2+sqrt(5)/2
1+sqrt(2)/2
-7/2+3*sqrt(5)/2

Hint

[Sample #2]

See uqe/uqe2.in and uqe/uqe2.ans in the attached files.

[Constraints]

For all data: 1T50001 \leq T \leq 5000, 1M1031 \leq M \leq 10 ^ 3, a,b,cM|a|,|b|,|c| \leq M, and a0a \neq 0.

Test Point ID MM \leq Special Property A Special Property B Special Property C
11 Yes
22 2020 No No No
33 10310 ^ 3 Yes Yes
44 No
55 No Yes Yes
66 No
7,87, 8 No Yes
9,109, 10 No

Where:

  • Special Property A: guarantees b=0b = 0.
  • Special Property B: guarantees c=0c = 0.
  • Special Property C: if the equation has solutions, then both solutions are integers.

Translated by ChatGPT 5