浅谈矩阵加速
miller2014 · · 算法·理论
1 单位矩阵
一般用
代码如下:
struct node
{
int a[N][N];
node(){memset(a,0,sizeof(a));for(int i=1;i<N;i++)a[i][i]=1;}
}
但是一般情况下初始化为全
2 同型矩阵
对于一个
代码如下:
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 加法
对于一个
代码如下:
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 减法
对于一个
代码如下:
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 乘法
对于一个
这也就是人们常说的左行右列。 代码如下:
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 运算律
- 矩阵满足加法交换律,即
A+B=B+A 。 - 矩阵满足加法结合律,即
A+B+C=A+(B+C) 。 - 矩阵满足减法的性质,即
A-B-C=A-(B+C) 且A-B+C=A-(B-C) 。 - 矩阵满足乘法结合律,即
A \times B \times C=A \times(B \times C) 。 - 矩阵不满足乘法交换律,即
A \times B \ne B \times A 。5 快速幂
因为矩阵满足乘法,所以我们可以利用快速幂优化。这样就可以在
O(n^3 \log b) 的时间复杂度下求出一个n \times n 的矩阵A 的b 次方,也就是A^b 。
代码如下:
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 斐波那契数列
题目链接。
设答案矩阵为
现在需要构造转移矩阵
初始化
#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 矩阵加速(数列)
题目链接。
设
代码如下:
#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 取模。设:
则转移方程为:
-
dp_{i,0}=2 \times dp_{i-1,0}+dp_{i-1,1}+dp_{i-1,2} -
dp_{i,1}=2 \times dp_{i-1,1}+dp_{i-1,0}+dp_{i-1,3} -
dp_{i,2}=2 \times dp_{i-1,2}+dp_{i-1,0}+dp_{i-1,3} -
dp_{i,3}=2 \times dp_{i-1,3}+dp_{i-1,1}+dp_{i-1,2}
可以构造答案矩阵
代码如下:
#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;
}