structMat { int n, m; vector<vector<int> > a; Mat(): n(0), m(0), a(vector<vector<int> >()) {} Mat(int n, int m): n(n), m(m), a(vector<vector<int> >(n + 1, vector<int>(m + 1))) {} vector<int>& operator[](int x) & { return a[x]; } const vector<int>& operator[](int x) const& { return a[x]; } Mat operator*(const Mat &b) const { Mat r(n, b.m); for (int i = 1; i <= n; i ++) for (int k = 1; k <= m; k ++) { for (int j = 1; j <= b.m; j ++) r[i][j] = ((L)r[i][j] + (L)a[i][k] * (L)b[k][j] % P + P) % P; } return r; } Mat operator^(int k) const { Mat r(0, 0), x = *this; for (; k; k >>= 1, x = x * x) { if (k & 1) r = (!r.n) ? r = x : r * x; } return r; } };
这么 sb 的代码有什么 sb 的功能吗?有!!!来康!
1 2 3 4 5
Mat foo(10, 10); // 通过构造函数定义大小,数组从 1 开始 foo[1][1] = 114514; // 重载中括号运算符,直接访问数组 Mat a(3, 5), b(4, 3); Mat c = a * b; // 直接乘! Mat c = a ^ 3// 矩阵快速幂!!!