浅谈数学中的矩阵

· · 算法·理论

矩阵,一种类似于二维数组的东西,样式大概为这种样子:

\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

只要是类型,那么就会有其对应的运算,那么它有哪些运算方式呢?

我们知道了求出a_x需要a_{x - 1}与a_{x - 3},那能不能给出一个矩阵,将原状态与它进行运算即可求出下一个状态呢?欸,有的:

\begin{bmatrix} a_{x - 1}\\a_{x - 2}\\a_{x - 3} \end{bmatrix} \cdot \begin{bmatrix} 1&0&1\\1&0&0\\0&1&0 \end{bmatrix} = \begin{bmatrix} a_{x+1}+a_{x-3}\\a_{x-1}\\a_{x-2} \end{bmatrix} = \begin{bmatrix} a_{x}\\a_{x-1}\\a_{x-2} \end{bmatrix}

至此,我们即可求出状态转移矩阵为\begin{bmatrix} 1&0&1\\1&0&0\\0&1&0 \end{bmatrix}。
顺便给大家分享我写的矩阵模板:

class ju {
    public:
        int h, l;
        int *juzhen;

        ju(int hang, int lie): h(hang), l(lie) {
            juzhen = new int[h * l];
            for(int i = 0; i < h * l; i++) {
                juzhen[i] = 0;
            }
        }

        ju(int hang, int lie, initializer_list<int> values): h(hang), l(lie) {
            juzhen = new int[h * l];
            int i = 0;
            for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
                juzhen[i] = *it;
            }
        }

        ~ju() {
            delete[] juzhen;
        }

        ju(const ju& other): h(other.h), l(other.l) {
            juzhen = new int[h * l];
            for(int i = 0; i < h * l; i++) {
                juzhen[i] = other.juzhen[i];
            }
        }

        void setValues(initializer_list<int> values) {
            int i = 0;
            for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
                juzhen[i] = *it;
            }
        }

        int& at(int row, int col) {
            return juzhen[row * l + col];
        }

        void print() {
            for(int i = 0; i < h; i++) {
                for(int j = 0; j < l; j++) {
                    cout << juzhen[i * l + j] << " ";
                }
                cout << endl;
            }
        }

        ju operator + (ju& others) {
            if(h != others.h || l != others.l) {
                throw invalid_argument("Matrix dimensions must match for addition");
            }

            ju temp(h, l);
            for (int i = 0; i < h * l; i++) {
                temp.juzhen[i] = juzhen[i] + others.juzhen[i];
            }
            return temp;
        }

        ju operator - (ju& others) {
            if(h != others.h || l != others.l) {
                throw invalid_argument("Matrix dimensions must match for addition");
            }
            ju temp(h, l);
            for (int i = 0; i < h * l; i++) {
                temp.juzhen[i] = juzhen[i] - others.juzhen[i];
            }
            return temp;
        }

        ju operator * (ju& others) {
            if(h != others.l) {
                throw invalid_argument("Matrix dimensions incompatible for multiplication");
            }

            ju result(h, others.l);
            for(int i = 0; i < h; i++) {
                for(int j = 0; j < others.l; j++) {
                    result.juzhen[i * others.l + j] = 0;
                    for(int k = 0; k < l; k++) {
                        result.juzhen[i * others.l + j] += juzhen[i * l + k] * others.juzhen[k * others.l + j];
                    }
                }
            }
            return result;
        }

        ju operator * (int others) {
            ju result(h, l);
            for(int i = 0; i < h; i++) {
                for(int j = 0; j < l; j++) {
                    result.juzhen[i * l + j] = juzhen[i * l + j] * others;
                }
            }
            return result;
        }

        ju& operator=(const ju& other) {
            if(this != &other) {
                delete[] juzhen;
                h = other.h;
                l = other.l;
                juzhen = new int[h * l];
                for(int i = 0; i < h * l; i++) {
                    juzhen[i] = other.juzhen[i];
                }
            }
            return *this;
        }
};

类的功能:

有了这些东西,可以让我们更好地写矩阵题。
话说回来,这道题只需要写出状态转移矩阵在dp就行了。 ::::warning[30-Score Code]{open}

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1e9 + 7;

class ju {
public:
    long long h, l;
    long long *juzhen;

    ju(long long hang, long long lie): h(hang), l(lie) {
        juzhen = new long long[h * l]();
    }

    ju(long long hang, long long lie, initializer_list<long long> values): h(hang), l(lie) {
        juzhen = new long long[h * l];
        long long i = 0;
        for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
            juzhen[i] = *it % MOD;
        }
    }

    ~ju() {
        delete[] juzhen;
    }

    void setValues(initializer_list<long long> values) {
        long long i = 0;
        for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
            juzhen[i] = *it % MOD;
        }
    }

    long long& at(long long row, long long col) {
        return juzhen[row * l + col];
    }

    void print() {
        for(long long i = 0; i < h; i++) {
            for(long long j = 0; j < l; j++) {
                cout << juzhen[i * l + j] << " ";
            }
            cout << endl;
        }
    }

    ju operator + (ju& others) {
        if(h != others.h || l != others.l) {
            throw invalid_argument("Matrix dimensions must match for addition");
        }

        ju temp(h, l);
        for (long long i = 0; i < h * l; i++) {
            temp.juzhen[i] = (juzhen[i] + others.juzhen[i]) % MOD;
        }
        return temp;
    }

    ju operator - (ju& others) {
        if(h != others.h || l != others.l) {
            throw invalid_argument("Matrix dimensions must match for addition");
        }
        ju temp(h, l);
        for (long long i = 0; i < h * l; i++) {
            temp.juzhen[i] = (juzhen[i] - others.juzhen[i] + MOD) % MOD;
        }
        return temp;
    }

    ju operator * (ju& others) {
        if(l != others.h) {
            throw invalid_argument("Matrix dimensions incompatible for multiplication");
        }

        ju result(h, others.l);
        for(long long i = 0; i < h; i++) {
            for(long long j = 0; j < others.l; j++) {
                long long sum = 0;
                for(long long k = 0; k < l; k++) {
                    sum = (sum + juzhen[i * l + k] * others.juzhen[k * others.l + j]) % MOD;
                }
                result.juzhen[i * others.l + j] = sum;
            }
        }
        return result;
    }

    ju operator * (long long others) {
        ju result(h, l);
        for(long long i = 0; i < h * l; i++) {
            result.juzhen[i] = (juzhen[i] * (others % MOD)) % MOD;
        }
        return result;
    }

    ju(const ju& other): h(other.h), l(other.l) {
        juzhen = new long long[h * l];
        for(long long i = 0; i < h * l; i++) {
            juzhen[i] = other.juzhen[i] % MOD;
        }
    }

    ju& operator=(const ju& other) {
        if(this != &other) {
            delete[] juzhen;
            h = other.h;
            l = other.l;
            juzhen = new long long[h * l];
            for(long long i = 0; i < h * l; i++) {
                juzhen[i] = other.juzhen[i] % MOD;
            }
        }
        return *this;
    }
};

ju change(3, 3, {1, 0, 1, 1, 0, 0, 0, 1, 0});
ju zh(3, 1, {1, 1, 1});

int main() {
    long long t;
    cin >> t;
    while (t--) {
        long long n;
        cin >> n;
        if (n <= 3) {
            cout << 1 << '\n';
        } else {
            ju result(3, 3);
            for(int i = 0; i < 3; i++) result.at(i,i) = 1;

            ju base = change;
            long long power = n - 3;
            while(power > 0) {
                if(power & 1) {
                    result = result * base;
                }
                base = base * base;
                power >>= 1;
            }
            ju final = result * zh;
            cout << final.juzhen[0] % MOD << '\n';
        }
    }
    return 0;
}

:::: 如果你提交,会发现只对了三个测试点,其它都是TLE。这时就要用到快速幂了。 ::::success[AC Code]{open}

#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1e9 + 7;

class ju {
public:
    long long h, l;
    long long *juzhen;

    ju(long long hang, long long lie): h(hang), l(lie) {
        juzhen = new long long[h * l]();
    }

    ju(long long hang, long long lie, initializer_list<long long> values): h(hang), l(lie) {
        juzhen = new long long[h * l];
        long long i = 0;
        for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
            juzhen[i] = *it % MOD;
        }
    }

    ~ju() {
        delete[] juzhen;
    }

    void setValues(initializer_list<long long> values) {
        long long i = 0;
        for(auto it = values.begin(); it != values.end() && i < h*l; ++it, ++i) {
            juzhen[i] = *it % MOD;
        }
    }

    long long& at(long long row, long long col) {
        return juzhen[row * l + col];
    }

    void print() {
        for(long long i = 0; i < h; i++) {
            for(long long j = 0; j < l; j++) {
                cout << juzhen[i * l + j] << " ";
            }
            cout << endl;
        }
    }

    ju operator + (ju& others) {
        if(h != others.h || l != others.l) {
            throw invalid_argument("Matrix dimensions must match for addition");
        }

        ju temp(h, l);
        for (long long i = 0; i < h * l; i++) {
            temp.juzhen[i] = (juzhen[i] + others.juzhen[i]) % MOD;
        }
        return temp;
    }

    ju operator - (ju& others) {
        if(h != others.h || l != others.l) {
            throw invalid_argument("Matrix dimensions must match for addition");
        }
        ju temp(h, l);
        for (long long i = 0; i < h * l; i++) {
            temp.juzhen[i] = (juzhen[i] - others.juzhen[i] + MOD) % MOD;
        }
        return temp;
    }

    ju operator * (ju& others) {
        if(l != others.h) {
            throw invalid_argument("Matrix dimensions incompatible for multiplication");
        }

        ju result(h, others.l);
        for(long long i = 0; i < h; i++) {
            for(long long j = 0; j < others.l; j++) {
                long long sum = 0;
                for(long long k = 0; k < l; k++) {
                    sum = (sum + juzhen[i * l + k] * others.juzhen[k * others.l + j]) % MOD;
                }
                result.juzhen[i * others.l + j] = sum;
            }
        }
        return result;
    }

    ju operator * (long long others) {
        ju result(h, l);
        for(long long i = 0; i < h * l; i++) {
            result.juzhen[i] = (juzhen[i] * (others % MOD)) % MOD;
        }
        return result;
    }

    ju(const ju& other): h(other.h), l(other.l) {
        juzhen = new long long[h * l];
        for(long long i = 0; i < h * l; i++) {
            juzhen[i] = other.juzhen[i] % MOD;
        }
    }

    ju& operator=(const ju& other) {
        if(this != &other) {
            delete[] juzhen;
            h = other.h;
            l = other.l;
            juzhen = new long long[h * l];
            for(long long i = 0; i < h * l; i++) {
                juzhen[i] = other.juzhen[i] % MOD;
            }
        }
        return *this;
    }
};

ju change(3, 3, {1, 0, 1, 1, 0, 0, 0, 1, 0});
ju zh(3, 1, {1, 1, 1});

int main() {
    long long t;
    cin >> t;
    while (t--) {
        long long n;
        cin >> n;
        if (n <= 3) {
            cout << 1 << '\n';
        } else {
            ju result(3, 3);
            for(int i = 0; i < 3; i++) result.at(i,i) = 1;

            ju base = change;
            long long power = n - 3;
            while(power > 0) {
                if(power & 1) {
                    result = result * base;
                }
                base = base * base;
                power >>= 1;
            }
            ju final = result * zh;
            cout << final.juzhen[0] << '\n';
        }
    }
    return 0;
}

::::warning{open} 该代码 仅供参考! :::: 以下即是我对矩阵的全部理解,感谢观看!(OVO)