b032困難

90 全國資訊學科能力競賽(2001)|反應式平衡

精確求唯一比例的最簡正係數。

解題思路

解析每個化合物的單一大寫元素及後續數字,下標省略為1;同一元素若多次出現要相加。反應物原子數取正、生成物取負,建立元素守恆矩陣。

化合物比元素多1且比例唯一,令最後係數為1,把最後一欄移到右側,用精確有理數高斯消去求其他係數。正比例解保證這個自由變數不為0。

取全部分母的最小公倍數化為整數,再除所有係數的gcd並統一正號。輸出保留原化合物文字、係數1省略,+與=前後各一空白。

元素最多26種,O(E³)次任意精度有理數運算;矩陣O(E²),數字儲存及位元成本依下標長度與中間分子分母而定。

Step-by-step animation

有理數高斯消去與整數核空間|A2+B2=AB,原子守恆為2x−z=0、2y−z=0。

回報動畫錯誤
步驟 1 / 3

方程

A2+B2=AB,原子守恆為2x−z=0、2y−z=0。

2x=z
2y=z
自由變數
z=1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
using I = boost::multiprecision::cpp_int;
using R = boost::rational<I>;
I gcdI(I a, I b) {
    if (a < 0)
        a = -a;
    if (b < 0)
        b = -b;
    while (b != 0) {
        I t = a % b;
        a = b;
        b = t;
    }
    return a;
}
int main() {
    string line, s;
    getline(cin, line);
    for (char c : line)
        if (!isspace(c))
            s += c;
    vector<string> terms;
    vector<int> sign;
    int side = 1, start = 0;
    for (int i = 0; i <= (int)s.size(); i++)
        if (i == (int)s.size() || s[i] == '+' || s[i] == '=') {
            terms.push_back(s.substr(start, i - start));
            sign.push_back(side);
            if (i < (int)s.size() && s[i] == '=')
                side = -1;
            start = i + 1;
        }
    int n = terms.size();
    vector<map<char, I>> atoms(n);
    set<char> elements;
    for (int j = 0; j < n; j++)
        for (int i = 0; i < (int)terms[j].size();) {
            char c = terms[j][i++];
            I count = 0;
            bool digits = false;
            while (i < (int)terms[j].size() && isdigit(terms[j][i])) {
                count = count * 10 + terms[j][i++] - '0';
                digits = true;
            }
            if (!digits)
                count = 1;
            atoms[j][c] += count;
            elements.insert(c);
        }
    vector<vector<R>> a;
    for (char c : elements) {
        vector<R> row(n);
        for (int j = 0; j < n; j++)
            row[j] = R(atoms[j][c] * sign[j]);
        row[n - 1] = -row[n - 1];
        a.push_back(row);
    }
    for (int c = 0; c < n - 1; c++) {
        int p = c;
        while (a[p][c].numerator() == 0)
            p++;
        swap(a[p], a[c]);
        R v = a[c][c];
        for (int j = c; j < n; j++)
            a[c][j] /= v;
        for (int i = 0; i < n - 1; i++)
            if (i != c) {
                v = a[i][c];
                for (int j = c; j < n; j++)
                    a[i][j] -= v * a[c][j];
            }
    }
    vector<R> x(n, R(1));
    for (int i = 0; i < n - 1; i++)
        x[i] = a[i][n - 1];
    I den = 1;
    for (auto v : x)
        den = den / gcdI(den, v.denominator()) * v.denominator();
    vector<I> ans;
    I g = 0;
    for (auto v : x) {
        I z = v.numerator() * (den / v.denominator());
        ans.push_back(z);
        g = gcdI(g, z);
    }
    for (int i = 0; i < n; i++) {
        if (i)
            cout << (sign[i] == sign[i - 1] ? " + " : " = ");
        I v = ans[i] / g;
        if (v < 0)
            v = -v;
        if (v != 1)
            cout << v;
        cout << terms[i];
    }
    cout << "\n";
}

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

更多