b333困難

92 臺北市資訊學科能力競賽(2003)|夕陽問題

將海平面上方圓面積正確捨入至兩位。

解題思路

將海平面上方圓面積正確捨入至兩位。

若r=0或y≤−r面積0;y≥r時為πr²。其餘面積是r²(π/2+atan(y/sqrt(r²−y²)))+y sqrt(r²−y²),x不影響。

用大整數平方根包圍sqrt,用Machin式π=16atan(1/5)−4atan(1/239)及交錯Taylor級數包圍atan。先以倒數與π/4轉換把級數參數絕對值壓至1/2以內。

以有理數區間運算得到面積上下界,只有兩端half-up相同才輸出;否則把精度加倍。有限十進位輸入精確解讀,避免極接近捨入界線時固定浮點失準。

令所需精度p位,Taylor項數 O(p),每輪為 O(p)次大有理數運算,精度倍增 O(log p)輪;位數成本另計。空間為有理數暫存,加上Python一次split保留的全部輸入字元。

Step-by-step animation

單位圓被海平面通過圓心的半圓示例

回報動畫錯誤
步驟 1 / 3

步驟 1

r=1,y=0,海平面通過圓心。

上方面積π/2
步驟
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;
}
using I = pair<Q, Q>;
I smallatan(Q x, Q eps) {
    if (x < 0) {
        auto z = smallatan(-x, eps);
        return {-z.second, -z.first};
    }
    Q term = x, sum(0), xx = x * x;
    int k = 0;
    while (true) {
        Q add = term / Q(2 * k + 1);
        if (k % 2)
            sum -= add;
        else
            sum += add;
        term *= xx;
        k++;
        Q next = term / Q(2 * k + 1);
        if (next < eps) {
            Q other = sum + (k % 2 ? -next : next);
            return {min(sum, other), max(sum, other)};
        }
    }
}
I atanpos(Q x, Q eps, I pi) {
    if (x > 1) {
        auto z = atanpos(Q(1) / x, eps, pi);
        return {pi.first / 2 - z.second, pi.second / 2 - z.first};
    }
    if (x > Q(1, 2)) {
        auto z = smallatan((x - 1) / (x + 1), eps);
        return {pi.first / 4 + z.first, pi.second / 4 + z.second};
    }
    return smallatan(x, eps);
}
int main() {
    string sx, sy, sr;
    while (cin >> sx >> sy >> sr) {
        Q y = dec(sy), r = dec(sr);
        if (r == 0 || y <= -r) {
            cout << "0.00\n";
            continue;
        }
        int digits = 8;
        while (true) {
            cpp_int T = ten(digits);
            Q eps(1, ten(digits + 8));
            auto a = smallatan(Q(1, 5), eps), b = smallatan(Q(1, 239), eps);
            I pi = {16 * a.first - 4 * b.second, 16 * a.second - 4 * b.first}, area;
            Q rr = r * r;
            if (y >= r)
                area = {rr * pi.first, rr * pi.second};
            else {
                Q ay = y < 0 ? -y : y;
                Q q = rr - ay * ay;
                cpp_int k = isq(q.numerator() * T * T / q.denominator());
                Q low(k, T), high(k + 1, T);
                if (k * k * q.denominator() == q.numerator() * T * T)
                    high = low;
                if (low == 0) {
                    digits *= 2;
                    continue;
                }
                auto al = atanpos(ay / high, eps, pi), ah = atanpos(ay / low, eps, pi);
                I cap = {rr * (pi.first / 2 + al.first) + ay * low,
                         rr * (pi.second / 2 + ah.second) + ay * high};
                area = y >= 0
                           ? cap
                           : I{rr * pi.first - cap.second, rr * pi.second - cap.first};
            }
            string lo = fixedq(area.first, 2), hi = fixedq(area.second, 2);
            if (lo == hi) {
                cout << lo << '\n';
                break;
            }
            digits *= 2;
        }
    }
}

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

更多