浅谈数学中的矩阵
Still_Shining · · 算法·理论
矩阵,一种类似于二维数组的东西,样式大概为这种样子:
只要是类型,那么就会有其对应的运算,那么它有哪些运算方式呢?
- 加法:
加法只有在两个矩阵行和列都一样的情况下才能运算。
将两个矩阵中每个位置的数字加起来即可。 例:A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} 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) - 减法
减法规则同上,但从加变成了减。例:\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) - 乘法 (难点)
-
- 数乘:
数乘只需要将矩阵中的每一个数都乘以这个数,例:\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}
- 数乘:
-
- 矩阵乘法 (难点):
乘法只有在
A 的列数等于B 的行数的情况下 才能运算。
它的答案C_{i,j} = \sum_{k = 1}^{m}A_{i,k}\cdot B_{k,j} 例:
\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} 说码话:
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)C\\ A(B + C) = AB + AC\\ AB ≠ BA 那么,这种集合类型在编程到底有什么用呢?
- 矩阵乘法 (难点):
乘法只有在
-
答:它可以快速的状态转移。
P1939 矩阵加速(数列)
题目大意:
a_x = \begin{cases} 1 & x \in \{1, 2, 3 \} \\ a_{x - 1} + a_{x - 3} & x ≥ 4\\ \end{cases} 然后T次询问,回答
a_x (取余1e9+7)。
我们知道了求出
至此,我们即可求出状态转移矩阵为
顺便给大家分享我写的矩阵模板:
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):返回juzhen_{i,j} 。print():打印矩阵。- 符号
+:矩阵加法。 - 符号
-:矩阵减法。 - 符号
*:- 数乘
- 矩阵乘法
- 符号
=:将右边的矩阵赋值给自己。(等价于(ju& others)构造函数。
有了这些东西,可以让我们更好地写矩阵题。
话说回来,这道题只需要写出状态转移矩阵在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)