跳到正文

算法·理论

浅谈数学中的矩阵

Still_Shining约 27 分钟
原文
文章信息与版本

文章信息

发布
2026年1月31日
收录
2026年2月2日
更新
2026年11月14日
篇幅
约 27 分钟

版本

  1. v2当前
  2. v1

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

[1234]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

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

  • 加法:
    加法只有在两个矩阵行和列都一样的情况下才能运算。
    将两个矩阵中每个位置的数字加起来即可。 例: A=[1234] A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} B=[5678] B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} A+B=[1+52+63+74+8]=[681012] A + B = \begin{bmatrix} 1 + 5 & 2 + 6 \\ 3 + 7 & 4 + 8 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ 10 & 12 \end{bmatrix} 矩阵加法满足交换律,结合律: A+B=B+A(A+B)+C=A+(B+C)A + B = B + A\\ (A + B) + C = A + (B + C)
  • 减法
    减法规则同上,但从加变成了减。例: [5678]−[1234]=[5−16−27−38−4]=[4444]\begin{bmatrix} 5&6\\7&8 \end{bmatrix} - \begin{bmatrix} 1&2\\3&4 \end{bmatrix}=\begin{bmatrix} 5-1&6-2\\7-3&8-4 \end{bmatrix} = \begin{bmatrix} 4&4\\4&4 \end{bmatrix} 矩阵减法满足连减性质: A−B−C=A−(B+C)A - B - C = A - (B + C)
  • 乘法 (难点)
    • 数乘:
      数乘只需要将矩阵中的每一个数都乘以这个数,例: [1234]⋅3=[1⋅32⋅33⋅34⋅3]=[36912]\begin{bmatrix} 1&2\\3&4 \end{bmatrix} \cdot 3 = \begin{bmatrix} 1\cdot3&2\cdot3\\3\cdot3&4\cdot3 \end{bmatrix}=\begin{bmatrix} 3&6\\9&12 \end{bmatrix}
    • 矩阵乘法 (难点): 乘法只有在 AA的列数等于BB的行数的情况下 才能运算。
      它的答案
    Ci,j=∑k=1mAi,k⋅Bk,jC_{i,j} = \sum_{k = 1}^{m}A_{i,k}\cdot B_{k,j} 例: [1234]⋅[5678]=[1⋅5+2⋅71⋅6+2⋅83⋅5+4⋅73⋅6+4⋅8]\begin{bmatrix} 1&2\\3&4 \end{bmatrix}\cdot\begin{bmatrix} 5&6\\7&8 \end{bmatrix}=\begin{bmatrix} 1\cdot5+2\cdot7&1\cdot6+2\cdot8\\3\cdot5+4\cdot7&3\cdot6+4\cdot8 \end{bmatrix} 说码话:
    C++
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < n; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    矩阵乘法满足结合律与分配律,但不满足交换律: A(BC)=(AB)CA(B+C)=AB+ACAB≠BAA(BC) = (AB)C\\ A(B + C) = AB + AC\\ AB ≠ BA

那么,这种集合类型在编程到底有什么用呢?

答:它可以快速的状态转移。

P1939 矩阵加速(数列)

题目大意:

ax={1x∈{1,2,3}ax−1+ax−3x≥4a_x = \begin{cases} 1 & x \in \{1, 2, 3 \} \\ a_{x - 1} + a_{x - 3} & x ≥ 4\\ \end{cases}

然后T次询问,回答axa_x(取余1e9+7)。

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

[ax−1ax−2ax−3]⋅[101100010]=[ax+1+ax−3ax−1ax−2]=[axax−1ax−2]\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}

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

C++
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;
		}
};

类的功能:

  • h, l分别存储了矩阵的行和列。
  • 指针矩阵juzhen一维模拟二维
  • 三种构造函数:
    • (int h, int l):仅给出行和列。
    • (int h, int l, initializer_list<int> values):在原来的基础上,对矩阵进行初赋值。(等价于第一种+setValues())
    • (ju& others):将others里的数据复制过来。
  • 析构函数:释放由new创造的空间
  • setValues(initializer_list<int> values)将values中的数值转移(h,l保持原样)
  • at(int i, int j):返回juzheni,jjuzhen_{i,j}。
  • print():打印矩阵。
  • 符号+:矩阵加法。
  • 符号-:矩阵减法。
  • 符号*:
    • 数乘
    • 矩阵乘法
  • 符号=:将右边的矩阵赋值给自己。(等价于(ju& others)构造函数。

有了这些东西,可以让我们更好地写矩阵题。
话说回来,这道题只需要写出状态转移矩阵在dp就行了。

30-Score Code
C++
#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。这时就要用到快速幂了。

AC Code
C++
#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

该代码 仅供参考!

以下即是我对矩阵的全部理解,感谢观看!(OVO)

原文