矩阵加速模板

· · 算法·理论

#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;

size_t mul(size_t x, size_t y) {
    size_t ans = 0;
    while (y) {
        if (y & 1) (ans += x) %= mod;
        (x += x) %= mod;
        y >>= 1;
    }
    return ans;
}

class matrix {
    size_t _height;
    size_t _width;
    vector<vector<size_t> > _matrix;

public:
    matrix(): _height(0), _width(0), _matrix(0) {
    }

    matrix(const size_t height, const size_t width): _height(0), _width(0), _matrix(0) {
        _height = height;
        _width = width;
        _matrix = vector(height, vector<size_t>(width));
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                _matrix[i][j] = 0;
            }
        }
    }

    matrix(const matrix &x): _height(0), _width(0), _matrix(0) {
        _height = x._height;
        _width = x._width;
        _matrix = x._matrix;
    }

    matrix(initializer_list<long long> x): _height(0), _width(0), _matrix(0) {
        _height = 1;
        _width = x.size();
        auto i = 0;
        for (auto j: x) {
            _matrix[0][i++] = j;
        }
    }

    matrix(initializer_list<initializer_list<long long> > x): _height(0), _width(0), _matrix(0) {
        _height = x.size();
        _width = x.begin()->size();

        for (auto j: x) {
            if (j.size() != _width)throw logic_error("Matrix_width_error");
        }
        _matrix = vector<vector<size_t> >(_height, vector<size_t>(_width));
        int i = 0;
        for (auto p: x) {
            int j = 0;
            for (auto q: p) {
                _matrix[i][j++] = q;
            }
            i++;
        }
    }

    friend matrix operator*(matrix a, matrix b) {
        matrix ans(a._height, b._width);
        for (int i = 0; i < a._height; i++) {
            for (int j = 0; j < b._width; j++) {
                ans[i][j] = 0;
                for (int k = 0; k < a._width; k++) {
                    ans[i][j] += (mul(a._matrix[i][k], b._matrix[k][j])) % mod;
                    ans[i][j] %= mod;
                }
            }
        }
        return ans;
    }

    friend matrix operator+(matrix &a, matrix &b) {
        if (a.width() != b.width())throw logic_error("Matrix_width_error");
        if (a.height() != b.height())throw logic_error("Matrix_height_error");
        matrix ans(a._height, a._width);
        for (int i = 0; i < a._height; i++) {
            for (int j = 0; j < a._width; j++) {
                ans[i][j] = a[i][j] + b[i][j];
            }
        }
        return ans;
    }

    friend matrix operator-(matrix &a, matrix &b) {
        if (a.width() != b.width())throw logic_error("Matrix_width_error");
        if (a.height() != b.height())throw logic_error("Matrix_height_error");
        matrix ans(a._height, a._width);
        for (int i = 0; i < a._height; i++) {
            for (int j = 0; j < a._width; j++) {
                ans[i][j] = a[i][j] - b[i][j];
            }
        }
        return ans;
    }

    vector<size_t> &operator[](const size_t &x) {
        return _matrix[x];
    }

    matrix &operator=(initializer_list<long long> x) {
        _matrix.clear();
        _height = 1;
        _width = x.size();
        auto i = 0;
        for (auto j: x) {
            _matrix[0][i++] = j;
        }
        return *this;
    }

    matrix &operator=(initializer_list<initializer_list<long long> > x) {
        _height = x.size();
        _width = x.begin()->size();

        for (auto j: x) {
            if (j.size() != _width)throw logic_error("Matrix_width_error");
        }
        _matrix = vector<vector<size_t> >(_height, vector<size_t>(_width));
        int i = 0;
        for (auto p: x) {
            int j = 0;
            for (auto q: p) {
                _matrix[i][j++] = q;
            }
            i++;
        }
        return *this;
    }

    [[nodiscard]] matrix pow(long long x) const{
        if (_height != _width)throw logic_error("Matrix_size_error");
        matrix ans(_height, _width);
        for (int i = 0; i < ans._height; i++) {
            ans[i][i] = 1;
        }
        matrix temp = *this;
        while (x) {
            if (x & 1)ans = ans * (temp);
            temp = temp * temp;
            x >>= 1;
        }
        return ans;
    }

    [[nodiscard]] size_t height() const{
        return _height;
    }

    [[nodiscard]] size_t width() const{
        return _width;
    }
};