P9750

· · 题解

大模拟。

考场上最重要的就是理清思路,我们先把此题所有的细节梳理一遍。

自此本题所有的情况列出,那么代码就迎刃而解了。

本人赛时半个小时码出,此题必须要求头脑清醒。

#include<bits/stdc++.h>
using namespace std;
#define int long long
int M;
namespace Jelly {
    const int N = 1e6 + 5, inf = INT_MAX;
    void print(int x, int y) {
        if(x == 0) return ;
        if(x < 0 && y < 0) x = -x, y = -y;
        else if(x < 0) x = -x, cout << '-';
        else if(y < 0) y = -y, cout << '-';
        int k = __gcd(x, y);
        if(y / k == 1) cout << x / k;
        else cout << x / k << '/' << y / k;
    }
    int main() {
        int a, b, c;
        cin >> a >> b >> c;
        int delta = b * b - 4 * a * c;
//      cout << delta << '\n';
        if(delta < 0) {
            cout << "NO\n";
            return 0;
        }
//      (-b + sqrt(delta)) / 2a
        int t = sqrt(delta);
        if(t * t == delta) {
            if(a > 0) {
                if(-b + t == 0) cout << 0;
                else print(-b + t, 2 * a);
            } else {
                if(-b - t == 0) cout << 0;
                else print(-b - t, 2 * a);
            }
            cout << '\n';
        } else {
            int xishu = 1;
            for(int i = t; i >= 2; i --) {
                if(delta % (i * i) == 0) xishu *= i, delta /= (i * i);
            }
            print(-b, 2 * a);
            if(b != 0) cout << '+';
//          cout << xishu << ' ' << delta << '\n';
            if(a < 0) a = -a;
            int k = __gcd(2 * a, xishu);
            xishu /= k;
            if(xishu != 1) cout << xishu << '*';
            cout << "sqrt(" << delta << ")";
            if(2 * a / k != 1) cout << '/' << 2 * a / k;
            cout << '\n';
        }
        return 0;
    }
}
signed main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T = 1;
    cin >> T >> M;
    while(T --) Jelly::main();
    return 0;
}