b188中等

96 雲嘉區資訊學科能力競賽(2007)|稀疏矩陣乘法與運算次數

每個C[i,j]枚舉中介索引k,只有A[i,k]與B[k,j]都不為0才計算乘積。

解題思路

每個C[i,j]枚舉中介索引k,只有A[i,k]與B[k,j]都不為0才計算乘積。

若該格有q個非零乘積,乘法計q次,加法計max(q-1,0)次,因第一個乘積可直接賦值;即使最後相消也不扣次數。

用十進位整數係數表示元素,精確相乘相加,依輸入名稱與規定空行列印原矩陣、乘積及次數。

尺寸m×n及n×p,時間 O(mnp)次任意精度十進位運算,空間 O(mn+np+mp)個數值,位數成本另計。

Step-by-step animation

教學小例:精確小數矩陣乘法與非零項計數

回報動畫錯誤
步驟 1 / 3

A=[1,-1]、B=[1;1]

兩個因子對皆非零

兩個因子對皆非零
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
cpp_int ten(int k) {
    cpp_int x = 1;
    while (k--)
        x *= 10;
    return x;
}

struct Dec {
    cpp_int n;
    int s;
    Dec(cpp_int a = 0, int b = 0) : n(a), s(b) {
    }
    Dec(string x) {
        s = 0;
        n = 0;
        bool neg = false, point = false;
        for (char c : x) {
            if (c == '-')
                neg = true;
            else if (c == '.')
                point = true;
            else if (isdigit(c)) {
                n = n * 10 + c - '0';
                s += point;
            }
        }
        if (neg)
            n = -n;
    }
    string str() const {
        cpp_int q = n;
        bool neg = q < 0;
        if (neg)
            q = -q;
        string x = q.convert_to<string>();
        if (s) {
            if ((int)x.size() <= s)
                x = string(s + 1 - x.size(), '0') + x;
            x.insert(x.size() - s, 1, '.');
            while (x.back() == '0')
                x.pop_back();
            if (x.back() == '.')
                x.pop_back();
        }
        if (neg && q != 0)
            x = "-" + x;
        return x;
    }
};

Dec operator+(Dec a, Dec b) {
    int s = max(a.s, b.s);
    return Dec(a.n * ten(s - a.s) + b.n * ten(s - b.s), s);
}

Dec operator-(Dec a, Dec b) {
    b.n = -b.n;
    return a + b;
}

Dec operator*(Dec a, Dec b) {
    return Dec(a.n * b.n, a.s + b.s);
}

bool operator<(Dec a, Dec b) {
    int s = max(a.s, b.s);
    return a.n * ten(s - a.s) < b.n * ten(s - b.s);
}

int main() {
    string an, bn, s;
    int m, n, k, p;
    cin >> an >> m >> n;
    vector<vector<Dec>> a(m, vector<Dec>(n));
    int za = 0, zb = 0;
    for (auto &r : a)
        for (auto &v : r) {
            cin >> s;
            v = Dec(s);
            za += v.n == 0;
        }
    cin >> bn >> k >> p;
    vector<vector<Dec>> b(k, vector<Dec>(p)), c(m, vector<Dec>(p));
    for (auto &r : b)
        for (auto &v : r) {
            cin >> s;
            v = Dec(s);
            zb += v.n == 0;
        }
    long long mul = 0, plus = 0;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < p; j++) {
            int q = 0;
            for (int z = 0; z < n; z++)
                if (a[i][z].n != 0 && b[z][j].n != 0) {
                    c[i][j] = c[i][j] + a[i][z] * b[z][j];
                    q++;
                }
            mul += q;
            plus += max(0, q - 1);
        }
    auto print = [&](vector<vector<Dec>> &v) {
        for (auto &r : v) {
            for (int i = 0; i < (int)r.size(); i++) {
                if (i)
                    cout << ' ';
                cout << r[i].str();
            }
            cout << '\n';
        }
    };
    cout << "Matrix " << an << " contains " << za << " (Zero)\n";
    print(a);
    cout << "\nMatrix " << bn << " contains " << zb << " (Zero)\n";
    print(b);
    cout << "\nMatrix C=" << an << 'X' << bn << '\n';
    print(c);
    cout << "\nUses\nMultiplication: " << mul << "\nAddition: " << plus << '\n';
}

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

更多