b290中等

103 彰雲嘉區資訊學科能力競賽|一元二次方程式的根

精確判斷根種類並將大小根正確捨入。

解題思路

精確判斷根種類並將大小根正確捨入。

先以大整數D=b²−4ac判斷正負;D<0無實根、D=0只有重根,分支不受輸出捨入影響。

D為完全平方時,根都是精確有理數,直接half-up。否則把sqrt(D)包在[k/T,(k+1)/T],其中k=floor sqrt(DT²),逐次擴大T。

對兩端代入根公式,若各自捨入相同即答案已被證明;否則提高精度。非平方D的根無理,不會恰為有理捨入中點,所以此程序必終止。按a正負決定兩根排序。

若需p位十進位精度,空間O(p+輸入位數),時間為O(log p)次大整數平方根與有理運算;無固定精度保證,p取決於輸入。

Step-by-step animation

精確判別式與根的區間捨入逐步追蹤

回報動畫錯誤
步驟 1 / 3

步驟 1

範例a=1,b=2,c=−1,D=8>0。

兩個不同實根
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
using ll = long long;
#include <boost/rational.hpp>
using Q = boost::rational<cpp_int>;
Q rat(cpp_int a, cpp_int b) {
    if (b < 0) {
        a = -a;
        b = -b;
    }
    return Q(a, b);
}
cpp_int abig(cpp_int a) {
    return a < 0 ? -a : a;
}
cpp_int ten(int n) {
    cpp_int a = 1;
    while (n--)
        a *= 10;
    return a;
}
Q dec(string s) {
    auto slash = s.find('/');
    if (slash != string::npos)
        return Q(cpp_int(s.substr(0, slash)), cpp_int(s.substr(slash + 1)));
    bool neg = !s.empty() && s[0] == '-';
    if (s[0] == '-' || s[0] == '+')
        s.erase(s.begin());
    auto p = s.find('.');
    int k = p == string::npos ? 0 : s.size() - p - 1;
    if (p != string::npos)
        s.erase(p, 1);
    size_t z = s.find_first_not_of('0');
    cpp_int a = z == string::npos ? cpp_int(0) : cpp_int(s.substr(z));
    return Q(neg ? -a : a, ten(k));
}
string fixedq(Q q, int digits) {
    cpp_int a = q.numerator(), b = q.denominator(), scale = ten(digits);
    bool neg = a < 0;
    a = abig(a);
    cpp_int k = (2 * a * scale + b) / (2 * b);
    string s = k.convert_to<string>();
    if (digits) {
        if (s.size() <= digits)
            s = string(digits + 1 - s.size(), '0') + s;
        s.insert(s.size() - digits, ".");
    }
    return (neg && k != 0 ? "-" : "") + s;
}
string finiteq(Q q) {
    cpp_int a = q.numerator(), b = q.denominator();
    bool neg = a < 0;
    a = abig(a);
    cpp_int whole = a / b, r = a % b;
    string s = whole.convert_to<string>();
    if (r != 0) {
        s += '.';
        while (r != 0) {
            r *= 10;
            cpp_int z = r / b;
            s += char('0' + z.convert_to<int>());
            r %= b;
        }
    }
    return (neg ? "-" : "") + s;
}
cpp_int isq(cpp_int n) {
    if (n <= 0)
        return 0;
    cpp_int x = cpp_int(1) << ((boost::multiprecision::msb(n) + 2) / 2);
    while (true) {
        cpp_int y = (x + n / x) / 2;
        if (y >= x)
            return x;
        x = y;
    }
}
string scaled(cpp_int k, int d) {
    bool neg = k < 0;
    if (neg)
        k = -k;
    string s = k.convert_to<string>();
    if (d) {
        if (s.size() <= d)
            s = string(d + 1 - s.size(), '0') + s;
        s.insert(s.size() - d, ".");
    }
    return (neg && k != 0 ? "-" : "") + s;
}
int main() {
    cpp_int a, b, c;
    cin >> a >> b >> c;
    cpp_int D = b * b - 4 * a * c;
    if (D < 0) {
        cout << "No real root\n";
        return 0;
    }
    if (D == 0) {
        cout << "Two same roots x=" << fixedq(rat(-b, 2 * a), 2) << '\n';
        return 0;
    }
    cpp_int k = isq(D), T = 1;
    string hi, lo;
    if (k * k == D) {
        Q r1 = rat(-b + k, 2 * a), r2 = rat(-b - k, 2 * a);
        if (r1 < r2)
            swap(r1, r2);
        hi = fixedq(r1, 2);
        lo = fixedq(r2, 2);
    } else {
        int digits = 8;
        while (true) {
            T = ten(digits);
            k = isq(D * T * T);
            Q low(k, T), up(k + 1, T);
            Q x1 = (Q(-b) + low) / Q(2 * a), x2 = (Q(-b) + up) / Q(2 * a),
              y1 = (Q(-b) - low) / Q(2 * a), y2 = (Q(-b) - up) / Q(2 * a);
            if (fixedq(x1, 2) == fixedq(x2, 2) && fixedq(y1, 2) == fixedq(y2, 2)) {
                if (a > 0) {
                    hi = fixedq(x1, 2);
                    lo = fixedq(y1, 2);
                } else {
                    hi = fixedq(y1, 2);
                    lo = fixedq(x1, 2);
                }
                break;
            }
            digits *= 2;
        }
    }
    cout << "Two different roots x1=" << hi << " , x2=" << lo << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多