b338中等

94 臺北市資訊學科能力競賽(2005)|球序列機率問題

精確求前N名桶序列的機率和。

解題思路

精確求前N名桶序列的機率和。

所有同長路徑有共同分母:第一顆球乘10,之後每個轉移與發射各乘10,因此可只用大整數分子比較。

每一時間、每個末桶只保留前N大分子。任何被淘汰前綴都有至少N條不更小且同末桶的前綴,接相同未來後綴仍不會變好,故不可能影響全域前N機率和。

逐步擴展轉移,分桶排序截斷;最後合併五桶取前N個。相同機率的字典序只決定哪條被選,對總和無影響;不足N條就No Solution。

L≤15、N≤2000,每筆時間 O(LN log N),工作空間 O(N)個O(L)位數的大整數。Python一次split另保留全部輸入字元。

Step-by-step animation

每個末桶保留前N大路徑機率逐步追蹤

回報動畫錯誤
步驟 1 / 3

步驟 1

序列WBB,固定首桶1,分子初值2、分母10。

首球白機率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;
}
int main() {
    vector<vector<pair<int, int>>> g = {{{1, 10}},
                                        {{0, 2}, {1, 4}, {2, 2}, {3, 2}},
                                        {{1, 3}, {2, 4}, {4, 3}},
                                        {{1, 8}, {3, 1}, {4, 1}},
                                        {{2, 7}, {3, 3}}};
    int white[] = {2, 8, 6, 3, 6};
    string s;
    int N;
    while (cin >> s >> N) {
        vector<vector<cpp_int>> d(5);
        d[0].push_back(s[0] == 'W' ? 2 : 8);
        cpp_int den = 10;
        for (int t = 1; t < (int)s.size(); t++) {
            vector<vector<cpp_int>> e(5);
            for (int u = 0; u < 5; u++)
                for (auto v : d[u])
                    for (auto [to, w] : g[u])
                        e[to].push_back(v * w *
                                        (s[t] == 'W' ? white[to] : 10 - white[to]));
            for (auto &v : e) {
                sort(v.rbegin(), v.rend());
                if (v.size() > N)
                    v.resize(N);
            }
            d = move(e);
            den *= 100;
        }
        vector<cpp_int> all;
        for (auto v : d)
            all.insert(all.end(), v.begin(), v.end());
        if (all.size() < N) {
            cout << "No Solution\n";
            continue;
        }
        sort(all.rbegin(), all.rend());
        cpp_int sum = 0;
        for (int i = 0; i < N; i++)
            sum += all[i];
        cout << fixedq(Q(sum, den), 8) << '\n';
    }
}

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

更多