2025.03.28

· · 个人记录

数论——矩阵快速幂

数学中的矩阵可以近似的看做计算机学中的二维数组。

前言

目前我们能碰到的矩阵主要有三种用途:

矩阵有自己的运算法则:

  1. 矩阵×数字

设矩阵A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},将A与任意数字x相加,可以得到:

A × x = \begin{bmatrix} 1x & 2x \\ 3x & 4x \end{bmatrix}
  1. 矩阵+矩阵

设矩阵A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},矩阵B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix},将A加上B,有:

A + B = \begin{bmatrix} 1+5 & 2+6 \\ 3+7 & 4+8 \end{bmatrix}

注意,矩阵+矩阵时需确保相加的两个矩阵大小一致,即行数和列数相同。

3.矩阵×矩阵

设矩阵A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},矩阵B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix},将A乘上B,有:

A × B = \begin{bmatrix} 5+14 & 6+16 \\ 15+28 & 18+32 \end{bmatrix}

设A × B = C,那么关于C,则有A的第i行每一个元素×B的第j行每一个元素等于C的第i行第j列。注意,需保证A的列数必须等于B的行数,否则无法相乘。

仅矩阵的左对角线上元素为1,其余为0的矩阵被称之为单位矩阵。

正题

矩阵有啥用,干嘛要学它?

我们不妨看一个例子——斐波那契数列。

斐波那契数列的定义:F_i = F_{i - 1} + F_{i - 2}。

那么,定义一个矩阵f = \begin{bmatrix} F_1 & F_2\end{bmatrix},如何让它变成矩阵\begin{bmatrix} F_2 & F_3\end{bmatrix}呢?

不难求出,F_2 = F_2,F_3 = F_1+F_2,导出关键矩阵C = \begin{bmatrix} 0 & 1 \\ 1 & 1\end{bmatrix}

f × C = \begin{bmatrix} F_2 & F_3\end{bmatrix}、f × C^2 = \begin{bmatrix} F_3 & F_4\end{bmatrix}、......、f × C ^{n - 2} = \begin{bmatrix} F_{n - 1} & F_n\end{bmatrix}

诶!套个快速幂,线性DPO(n)的斐波那契数列求解项就可以变成O(4*log(n))(4是矩阵乘法常数)了。

例题B2105 矩阵乘法

矩阵乘法板题,我学OI三个月都会做。

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
int n, m, k, a[N][N], b[N][N], ans[N][N];
struct Martix{
    int n, m;
    int a[N][N];
    void input(){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++){
                cin >>a[i][j];
            }
        }
        return ;
    }
    void  output(){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j ++){
                cout << a[i][j] << ' ';
            }
            cout << '\n';
        }
        return ;
    }
    Martix(int x, int y){
        n = x, m = y;
        memset(a, 0, sizeof a);
    }
    Martix operator *(Martix b){
        Martix c = Martix(n, b.m);
        c.n = n, c.m = b.m;
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= b.m; j ++){
                for(int k = 1; k <= m; k ++){
                    c.a[i][j] += (a[i][k] * b.a[k][j]);
                }
            }
        }
        return c;
    }
};
int main() {
    cin >> n >> m >> k;
    Martix A = Martix(n, m), B = Martix(m, k), ans = Martix(n, k);
    A.input(), B.input();
    ans = A * B;
    ans.output();
    return 0;
}

例题P3390 【模板】矩阵快速幂

矩阵乘法套快速幂板子即可。不会有人不会ksm吧

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int n, k;
struct Martix {
    int n, m;
    int a[110][110];
    Martix() {
        n = 0;
        m = 0;
        memset(a, 0, sizeof(a));
    }
    void input() {
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j <= m; j ++) {
                cin >>a[i][j];
            }
        }
        return ;
    }
    void  output() {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j ++) {
                cout << a[i][j] << ' ';
            }
            cout << '\n';
        }
        return ;
    }
    Martix(int k) {
        n = k;
        m = k;
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= k; i++) {
            a[i][i] = 1;
        }
    }
    Martix(int x, int y) {
        n = x;
        m = y;
        memset(a, 0, sizeof(a));
    }
    Martix operator*(Martix b) {
        Martix c(n, b.m);
        for (int i = 1; i <= c.n; i++) {
            for (int j = 1; j <= c.m; j++) {
                for (int k = 1; k <= m; k++) {
                    c.a[i][j] = (c.a[i][j] + (a[i][k] * b.a[k][j])) % mod;
                }
            }
        }
        return c;
    }
};
Martix quick_pow(Martix A, int b) {
    Martix ans(n);
    while (b) {
        if (b & 1) {
            ans = ans * A;
        }
        b >>= 1;
        A = A * A;
    }
    return ans;
}
signed main() {
    cin >> n >> k;
    Martix A(n, n);
    A.input();
    Martix ans = quick_pow(A, k);
    ans.output();
    return 0;
}

例题P1349 广义斐波那契数列

把之前求的C = \begin{bmatrix} 0 & q \\ 1 & p\end{bmatrix}即可,推导过程略。

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
int p, q, a1, a2, n, mod;
struct Martix {
    int n, m;
    int a[3][3];
    Martix() {
        n = 0;
        m = 0;
        memset(a, 0, sizeof(a));
    }
    void input() {
        for(int i = 1; i <= n; i ++) {
            for(int j = 1; j <= m; j ++) {
                cin >>a[i][j];
            }
        }
        return ;
    }
    void  output() {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j ++) {
                cout << a[i][j] << ' ';
            }
            cout << '\n';
        }
        return ;
    }
    Martix(int k) {
        n = k;
        m = k;
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= k; i++) {
            a[i][i] = 1;
        }
    }
    Martix(int x, int y) {
        n = x;
        m = y;
        memset(a, 0, sizeof(a));
    }
    Martix operator*(Martix b) {
        Martix c(n, b.m);
        for (int i = 1; i <= c.n; i++) {
            for (int j = 1; j <= c.m; j++) {
                for (int k = 1; k <= m; k++) {
                    c.a[i][j] = (c.a[i][j] + (a[i][k] * b.a[k][j])) % mod;
                }
            }
        }
        return c;
    }
};
Martix A(2, 2), B(2, 2);
void quick_pow(int b) {
    while (b) {
        if (b & 1) {
            A = A * B;
        }
        b >>= 1;
        B = B * B;
    }
}
signed main() {
    cin >> p >> q >> a1 >> a2 >> n >> mod;
    if(n == 1){
        cout << a1 % mod;
        return 0;
    }
    if(n == 2){
        cout << a2 % mod;
        return 0;
    }
    A.a[1][1] = a2, A.a[1][2] = a1;
    B.a[1][1] = p, B.a[2][1] = q, B.a[1][2] = 1;
    quick_pow(n - 2);
    cout << A.a[1][1] % mod;
    return 0;
}

例题U546616 骨牌

同理,容易发现ans_n = ans_{n - 1} + ans_{n - 2} + ans_{n - 3},将C = \begin{bmatrix} 0 & 0 & 1 \\ 1 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix}即可。

但是注意,n ≤ 3时特判一下。

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int N = 5; 
int n, ans;
struct Martix {
    int n, m;
    int a[N][N];
    Martix(int x = 0, int y = 0) : n(x), m(y) {
        memset(a, 0, sizeof(a));
    }
    Martix operator *(const Martix& b) const {
        Martix c(n, b.m);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= b.m; j++)
                for(int k = 1; k <= m; k++)
                    c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % MOD;
        return c;
    }
    void identity() {
        for(int i = 1; i <= n; i++)
            a[i][i] = 1;
    }
};
Martix matrix_pow(Martix a, int n) {
    Martix res(a.n, a.m);
    res.identity();
    while(n > 0) {
        if(n & 1) res = res * a;
        a = a * a;
        n >>= 1;
    }
    return res;
}
signed main() {
    cin >> n;
    if(n == 0) {
        cout << 0 << endl;
        return 0;
    }
    if(n == 1) {
        cout << 1 << endl;
        return 0;
    }
    if(n == 2) {
        cout << 2 << endl;
        return 0;
    }
    if(n == 3) {
        cout << 4 << endl;
        return 0;
    }
    Martix B(3, 3);
    B.a[1][1] = B.a[1][2] = B.a[1][3] = B.a[2][1] = B.a[3][2] = 1;;
    B.a[2][2] = B.a[2][3] = B.a[3][1] = B.a[3][3] = 0;
    Martix res = matrix_pow(B, n - 3);
    ans = (res.a[1][1] * 4 + res.a[1][2] * 2 + res.a[1][3] * 1) % MOD;
    cout << ans << endl;
    return 0;
}

例题P2044 [NOI2012] 随机数生成器

这题与斐波那契不同,X_{i + 1}与X_{i - 1}并无太大关系,我们可以更改原先的矩阵f,考虑到新加入了一个c会改变数列X的元素大小,于是将f = \begin{bmatrix} X_0 & 1 \end{bmatrix},C = \begin{bmatrix} a & 0 \\ c & 1\end{bmatrix},套版即可。 调了114514h,果断用上大佬的快速乘优化。

Code

#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
int mod, a, c, x0, n, g;
int qmul(int a, int b, int mod) {
    int res = 0;
    while (b) {
        if (b & 1) res = (res + a) % mod;
        a = (a << 1) % mod;
        b >>= 1;
    }
    return res;
}
struct Matrix {
    int m[2][2];
    Matrix() {
        memset(m, 0, sizeof(m));
    }
    Matrix operator*(const Matrix& B) const {
        Matrix res;
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                for (int k = 0; k < 2; k++)
                    res.m[i][j] = (res.m[i][j] + qmul(m[i][k], B.m[k][j], mod)) % mod;
        return res;
    }
};
Matrix matrix_pow(Matrix base, int power) {
    Matrix res;
    res.m[0][0] = res.m[1][1] = 1;
    while (power) {
        if (power & 1) res = res * base;
        base = base * base;
        power >>= 1;
    }
    return res;
}

signed main() {
    cin >> mod >> a >> c >> x0 >> n >> g;
    if (n == 0) {
        cout << x0 % g << endl;
        return 0;
    }
    Matrix trans;
    trans.m[0][0] = a % mod;
    trans.m[1][0] = c % mod;
    trans.m[1][1] = 1 % mod;
    Matrix pow_trans = matrix_pow(trans, n);
    int result = (qmul(pow_trans.m[0][0], x0, mod) + pow_trans.m[1][0]) % mod;
    cout << result % g << endl;
    return 0;
}