Analysis of past CSP-J semi-final exam questions | 2023 T3 quadratic equations

Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.

Attached is a summary post: Analysis of past CSP-J semi-final questions | Summary_Blog of a communicator who loves programming-CSDN Blog


[Title description]

As we all know, for the quadratic equation ax² + bx + c = 0, (a≠0), the real number solution can be found in the following way:

  • Calculate △=b²-4ac, then:

1. If △<0, then the quadratic equation has no real solution;

2. Otherwise △≥0, then the quadratic equation has two real solutions x1,2=(-b±√△)/2a;

- Among them, √△ represents the arithmetic square root of △, which is the only non-negative real number s that makes s² = △.

-Specially, when △=0, the two real solutions are equal; when △>0, the two real solutions are different from each other.

For example:

  • x²+x+1=0 has no real solution, because △=1²-4×1×1=-3<0;
  • x²-2x+1=0 has two equal real solutions x1,2=1;
  • x²-3x+2=0 has two mutually different real solutions x1=1, x2=2;

In the question description, the greatest common factor of a and b is represented by gcd(a,b). For example, the greatest common factor of 12 and 18 is 6, that is, gcd(12,18) = 6.

Now given the coefficients a, b, c of a quadratic equation of one variable, where a, b, c are all integers and a≠0. You need to determine whether the quadratic equation ax²+bx+c=0 has a real solution and output it in the required format.

The following rules must be followed when outputting the rational number v in this question:

  • According to the definition of rational numbers, there are only two integers p and q that satisfy q>0, gcd(p,q) =1 and v=p/q.
  • If q=1, then 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 respectively, then -1/2 should be output;
  • When v=0, the values ​​of p and q are 0 and 1 respectively, then 0 should be output.

For the solution of the equation, we will discuss it in two situations:

1. If △=b²-4ac<0, it means that the equation has no real solution, and you should output NO at this time;

2. Otherwise △≥0, then the equation has two solutions (which may be equal), and the larger one is x, then:

(1). If x is a rational number, output it in the format of a rational number.

(2). Otherwise, according to the above formula, x can be uniquely expressed in the form of x=q1+q2√r, where:

·q1, q2 are rational numbers, and q2>0;

·r is a positive integer and r>1, and there is no positive integer d>1 such that d²|r (that is, r should not be a multiple of d²);

at this time:

1. If q1≠0, output q1 in the format of a rational number, and then output a plus sign +;

2. Otherwise, skip this step of output;

Then:

1. If q2=1, then output sqrt({r});

2. Otherwise, if q2 is an integer, output {q2} * sqrt({r});

3. Otherwise, if q3=1/q2 is an integer, output sqrt({r}) /{q3};

4. Otherwise, it can be proved that there is a unique integer c, d that satisfies c, d>1, gcd(c,d) = 1 and q2=c/d. At this time, {c} * sqrt({r}) /{d} is output. ;

In the above expression, {n} represents the value of the integer n. See the example for details.

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

【enter】

The first line of input contains two positive integers T and M, which respectively represent the number of equations and the upper bound of the absolute value of the coefficient;

Next, T lines, each line contains three integers a, b, c.

【Output】

Output T lines. Each line contains a string representing the answer to the corresponding query. The format is as described in the question.

Each line of output string should not contain any spaces in the middle.

【Input sample】

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 sample】

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

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int t, m, a, b, c, delta, aa, bb, gcd1, gcd2, q2, r, ans;
int gcd(int a, int b)
{
    while (b!=0) {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main()
{
    cin >> t >> m;
    for (int i=1; i<=t; i++) {
        cin >> a >> b >> c;
        delta = b*b - 4*a*c;
        aa = 2*a;
        bb = -1*b;
        if (a<0) {  //当a<0时,对aa和bb进行处理,让a<0和a>0,都是统一解
            aa = -1 * aa;
            bb = -1 * bb;
        }
        q2 = 1, r = delta;
        for (int i=2; i*i<=r; i++) {  //对于任意的数,求出其q2和r。如果可开平方,则r=1
            while (r%(i*i)==0) {
                q2 = q2*i;
                r = r/(i*i);
            }
        }
        gcd1 = gcd(abs(bb), aa);
        gcd2 = gcd(q2, aa);
        if (r==1) {  //针对r==1进行特判,修改delta和bb的值
            delta = 0;
            bb = bb+q2;
        }
        if (delta<0) {
            cout << "NO" << endl;
            continue;
        }
        if (delta==0) {
            if (bb%aa==0) cout << bb/aa;
            else cout << bb/gcd1 << "/" << aa/gcd1;
        }
        else { //即delta>0的场景
            if (bb!=0) {  //前半部分的输出,如果bb==0,则没有输出
                if (bb%aa==0) cout << bb/aa;
                else cout << bb/gcd1 << "/" << aa/gcd1;
                cout << "+";
            }
            if (q2/gcd2!=1) cout << q2/gcd2 << "*";
            cout << "sqrt(" << r << ")";
            if (aa/gcd2!=1) cout << "/" << aa/gcd2;  //处理分母的技巧,==1就不输出
        }        
        cout << endl;
    }
    return 0;
}

【operation result】

9 1000
1 -1 0
1
-1 -1 -1
NO
1 -2 1
1
1 5 4
-1
4 4 1
-1/2
1 0 -432
12*sqrt(3)
1 -3 1
3/2+sqrt(5)/2
2 -4 1
1+sqrt(2)/2
1 7 1
-7/2+3*sqrt(5)/2

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/134038972