浅谈矩阵加速

· · 算法·理论

1 单位矩阵

一般用 I 来表示单位矩阵,就是主对角线上为 1,其余位置为 0。

代码如下:

struct node
{
    int a[N][N];
    node(){memset(a,0,sizeof(a));for(int i=1;i<N;i++)a[i][i]=1;}
}

但是一般情况下初始化为全 0 即可。

2 同型矩阵

对于一个 n \times m 的矩阵 A 与一个 n \times m 的矩阵 B,若满足以下条件则称它们是同型矩阵:

A_{i,j}=B_{i,j}

代码如下:

bool operator==(const node &b)const
{
    bool res=true;
    for(int i=1;i<=N-5;++i)
        for(int j=1;j<=N-5;++j)
            res&&=(a[i][j]==b.a[i][j]);
    return res;
}

3 运算

3.1 加法

对于一个 n \times m 的矩阵 A 与一个 n \times m 的矩阵 B,A+B 的结果就是一个 n \times m 的矩阵 C,计算公式如下:

C_{i,j}=A_{i,j}+B_{i,j}

代码如下:

node operator+(const node &b)const
{
    node res;
    for(int i=1;i<=N-5;++i)
        for(int j=1;j<=N-5;++j)
            res.a[i][j]=(a[i][j]+b.a[i][j])%M;
    return res;
}

3.2 减法

对于一个 n \times m 的矩阵 A 与一个 n \times m 的矩阵 B,A-B 的结果就是一个 n \times m 的矩阵 C,计算公式如下:

C_{i,j}=A_{i,j}-B_{i,j}

代码如下:

node operator-(const node &b)const
{
    node res;
    for(int i=1;i<=N-5;++i)
        for(int j=1;j<=N-5;++j)
            res.a[i][j]=(a[i][j]-b.a[i][j])%M;
    return res;
}

3.3 乘法

对于一个 n \times m 的矩阵 A 与一个 m \times q 的矩阵 B,A \times B 的结果就是一个 n \times q 的矩阵 C,计算公式如下:

C_{i,j}=\sum\limits_{k=1}^{m}A_{i,k} \times B_{k,j}

这也就是人们常说的左行右列。 代码如下:

node operator*(const node &b)const
{
    node res;
    for(int i=1;i<=N-5;++i)
        for(int j=1;j<=N-5;++j)
            for(int k=1;k<=N-5;++k)
                res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j])%M;
    return res;
}

4 运算律

代码如下:

node fpow(node x,int u)
{
    node ans;
    for(int i=1;i<=n;i++)ans.a[i][i]=1;
    while(u)
    {
        if(u&1)ans=ans*x;
        x=x*x,u/=2;
    }
    return ans;
}

6 例题 & 实现

6.1 【模板】矩阵快速幂

题目链接。

模板题,直接套用上述代码即可。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=105,M=1000000007;
int n,k;
struct node
{
    int a[N][N];
    node(){memset(a,0,sizeof(a));}
    node operator*(const node &b)const
    {
        node res;
        for(int i=1;i<=N-5;++i)
            for(int j=1;j<=N-5;++j)
                for(int k=1;k<=N-5;++k)
                    res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j])%M;
        return res;
    }
}a;
node mi(node x,int u)
{
    node ans;
    memset(ans.a,0,sizeof(ans.a));
    for(int i=1;i<N;i++)ans.a[i][i]=1;
    while(u)
    {
        if(u&1)ans=ans*x;
        x=x*x,u/=2;
    }
    return ans;
}
signed main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>a.a[i][j];
    a=mi(a,k);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)cout<<a.a[i][j]<<" ";
        cout<<"\n";
    }
    return 0;
}

6.2 斐波那契数列

题目链接。

设答案矩阵为 ans,因为想要知道 F_i 就需要知道 F_{i-1} 与 F_{i-2},所以设 ans=\begin{bmatrix}F_i & F_{i-1}\end{bmatrix}。

现在需要构造转移矩阵 A,因为 F_{i+1}=F_i+F_{i-1},所以 A_{1,1}=A_{2,1}=1。又因为 F_i=F_i,所以 A_{1,2}=1 且 A_{2,2}=0。

初始化 ans=\begin{bmatrix}F_2 & F_1\end{bmatrix}=\begin{bmatrix}1 & 1\end{bmatrix},并初始化 A=\begin{bmatrix}1 & 1\\1 & 0\end{bmatrix},计算 ans \times A^{n-2}=\begin{bmatrix}F_n & F_{n-1}\end{bmatrix},最后输出 ans_{1,1} 即可。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3,M=1000000007;
int n;
struct node
{
    int a[N][N];
    node(){memset(a,0,sizeof(a));}
    node operator*(const node &b)const
    {
        node res;
        for(int i=1;i<=2;++i)
            for(int j=1;j<=2;++j)
                for(int k=1;k<=2;++k)
                    res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j])%M;
        return res;
    }
}a,ans;
node mi(node x,int u)
{
    node ans;
    memset(ans.a,0,sizeof(ans.a));
    for(int i=1;i<N;i++)ans.a[i][i]=1;
    while(u)
    {
        if(u&1)ans=ans*x;
        x=x*x,u/=2;
    }
    return ans;
}
signed main()
{
    cin>>n;
    a.a[1][1]=a.a[1][2]=a.a[2][1]=1,ans.a[1][1]=ans.a[1][2]=1;
    if(n<3)return (cout<<"1")&&0;
    a=ans*mi(a,n-2);
    cout<<a.a[1][1];
    return 0;
}

6.3 矩阵加速(数列)

题目链接。

设 ans=\begin{bmatrix}a_i & a_{i-1} & a_{i-2}\end{bmatrix},那么根据刚才的方法,我们可以快速地推出转移矩阵 A=\begin{bmatrix}1 & 1 & 0\\0 & 0 & 1\\1 & 0 & 0\end{bmatrix},那么接下来只需要用 \begin{bmatrix}a_3 & a_2 & a_1\end{bmatrix} \times \begin{bmatrix}1 & 1 & 0\\0 & 0 & 1\\1 & 0 & 0\end{bmatrix}^{n-3}=\begin{bmatrix}a_n & a_{n-1} & a_{n-2}\end{bmatrix},这样我们就算出来了!

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5,M=1000000007;
int t,n;
struct node
{
    int a[N][N];
    node(){memset(a,0,sizeof(a));}
    node operator*(const node &b)const
    {
        node res;
        for(int i=1;i<N;++i)
            for(int j=1;j<N;++j)
                for(int k=1;k<N;++k)
                    res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j])%M;
        return res;
    }
}a,ans;
node mi(node x,int u)
{
    node ans;
    memset(ans.a,0,sizeof(ans.a));
    for(int i=1;i<N;i++)ans.a[i][i]=1;
    while(u)
    {
        if(u&1)ans=ans*x;
        x=x*x,u/=2;
    }
    return ans;
}
signed main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(ans.a,0,sizeof(ans.a)),memset(a.a,0,sizeof(a.a));
        ans.a[1][1]=ans.a[1][2]=ans.a[1][3]=1,a.a[1][1]=a.a[1][2]=a.a[2][3]=a.a[3][1]=1;
        if(n<4)
        {
            cout<<"1\n";
            continue;
        }
        ans=ans*mi(a,n-3);
        cout<<ans.a[1][1]<<"\n";
    }
    return 0;
}

6.4 偶数涂色

题目描述

熊猫接到了一项给一排方块涂色的任务。因为熊猫是个非常聪明的孩子,所以他开始思考一个关于涂色的数学问题。假设有一排共 N 个方块,每个方块可以涂成红色、蓝色、绿色或黄色。由于一些神秘的原因,熊猫希望红色方块和绿色方块的数量都为偶数。在这样的条件下,熊猫想知道给这些方块涂色的不同方法的数量。

输入格式

输入的第一行包含一个整数 T(1 \le T \le 100),表示测试用例的数量。接下来 T 行,每行都包含一个整数 N(1 \le N \le 10^9),表示方块的数量。

输出格式

对于每个测试用例,在一行中输出给方块涂色的方法数量。由于答案可能非常大,你必须将其对 10007 取模。

设:

则转移方程为:

可以构造答案矩阵 ans=\begin{bmatrix}dp_{i,0} & dp_{i,1} & dp_{i,2} & dp_{i,3} & \end{bmatrix} 与转移矩阵 A=\begin{bmatrix}2 & 1 & 1 & 0\\1 & 2 & 0 & 1\\1 & 0 & 2 & 1\\0 & 1 & 1 & 2\end{bmatrix},计算即可。

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
const int N=6,mod=10007;
struct node
{
    int a[N][N];
    node(){memset(a,0,sizeof(a));}
    node operator*(const node &b)const
    {
        node res;
        for(int i=1;i<N;++i)
            for(int j=1;j<N;++j)
                for(int k=1;k<N;++k)
                    res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j])%mod;
        return res;
    }
}ans,a;
node mi(node x,int u)
{
    node ans;
    memset(ans.a,0,sizeof(ans.a));
    for(int i=1;i<N;i++)ans.a[i][i]=1;
    while(u)
    {
        if(u&1)ans=ans*x;
        x=x*x,u/=2;
    }
    return ans;
}
signed main()
{
    cin>>n;
    while(cin>>n)
    {
        memset(ans.a,0,sizeof(ans.a)),memset(a.a,0,sizeof(a));
        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++)
                a.a[i][j]=1;
        ans.a[1][1]=0,ans.a[1][2]=0,ans.a[1][3]=0,ans.a[1][4]=1;
        a.a[1][1]=a.a[2][2]=a.a[3][3]=a.a[4][4]=2,a.a[1][4]=a.a[2][3]=a.a[3][2]=a.a[4][1]=0;
        ans=ans*mi(a,n);
        cout<<ans.a[1][4]<<"\n";
    }
    return 0;
}