跳到主要内容

矩阵

参考资料

例题

给定 n×nn\times n 的矩阵 AA,求 AkA^k

代码(1)
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int mod=1000000007;
const int N=105;
struct Mat
{
int n;
ll a[N][N];
Mat(){memset(a,0,sizeof a);n=0;}
Mat operator*(const Mat &rhs) const
{
Mat res;
res.n=n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
res.a[i][j]=(res.a[i][j]+a[i][k]*rhs.a[k][j])%mod;
}
}
}
return res;
}
Mat operator^(ll rhs) const
{
Mat res,tmp=*this;
res.n=n;
for(int i=0;i<n;i++)res.a[i][i]=1;
while(rhs)
{
if(rhs&1)res=res*tmp;
tmp=tmp*tmp;
rhs>>=1;
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll k;
Mat mat;
cin>>mat.n>>k;
for(int i=0;i<mat.n;i++)
{
for(int j=0;j<mat.n;j++)
{
cin>>mat.a[i][j];
}
}
mat=mat^k;
for(int i=0;i<mat.n;i++)
{
for(int j=0;j<mat.n;j++)
{
cout<<mat.a[i][j]<<' ';
}
cout<<'\n';
}
return 0;
}

已知一个数列 aa,它满足:

ax={1x{1,2,3}ax1+ax3x4a_x= \begin{cases} 1 & x \in\set{1,2,3} \\ a_{x-1}+a_{x-3} & x \geq 4 \end{cases}

aa 数列的第 nn 项对 109+710^9+7 取余的值。

代码(1)
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int mod=1e9+7;
const int N=3;
struct Mat
{
ll a[N][N];
Mat(){memset(a,0,sizeof a);}
Mat operator*(const Mat &rhs) const
{
Mat res;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
for(int k=0;k<N;k++)
{
res.a[i][j]=(res.a[i][j]+a[i][k]*rhs.a[k][j])%mod;
}
}
}
return res;
}
Mat operator^(ll rhs) const
{
Mat res,tmp=*this;
for(int i=0;i<N;i++)res.a[i][i]=1;
while(rhs)
{
if(rhs&1)res=res*tmp;
tmp=tmp*tmp;
rhs>>=1;
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--)
{
ll n;
cin>>n;
Mat mat;
mat.a[0][0]=1;mat.a[0][1]=0;mat.a[0][2]=1;
mat.a[1][0]=1;mat.a[1][1]=0;mat.a[1][2]=0;
mat.a[2][0]=0;mat.a[2][1]=1;mat.a[2][2]=0;
mat=mat^n-1;
cout<<mat.a[0][0]<<'\n';
}
return 0;
}

大家都知道,斐波那契数列是满足如下性质的一个数列:

Fn={1 (n2)Fn1+Fn2 (n3)F_n=\left\{ \begin{aligned} 1\space (n\le 2) \\ F_{n-1}+F_{n-2}\space(n\ge 3) \end{aligned}\right.

请你求出 Fnmod109+7F_n\bmod 10^9+7 的值。

代码(1)
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int mod=1e9+7;
const int N=2;
struct Mat
{
ll a[N][N];
Mat(){memset(a,0,sizeof a);}
Mat operator*(const Mat &rhs) const
{
Mat res;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
for(int k=0;k<N;k++)
{
res.a[i][j]=(res.a[i][j]+a[i][k]*rhs.a[k][j])%mod;
}
}
}
return res;
}
Mat operator^(ll rhs) const
{
Mat res,tmp=*this;
for(int i=0;i<N;i++)res.a[i][i]=1;
while(rhs)
{
if(rhs&1)res=res*tmp;
tmp=tmp*tmp;
rhs>>=1;
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin>>n;
Mat mat;
mat.a[0][0]=1;mat.a[0][1]=1;
mat.a[1][0]=1;mat.a[1][1]=0;
mat=mat^n-1;
cout<<mat.a[0][0]<<'\n';
return 0;
}

广义的斐波那契数列是指形如 an=p×an1+q×an2a_n=p\times a_{n-1}+q\times a_{n-2} 的数列。

今给定数列的两系数 ppqq,以及数列的最前两项 a1a_1a2 a_2,另给出两个整数 nnmm,试求数列的第 nnana_nmm 取模后的结果。

代码(1)
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int N=2;
ll a1,a2,mod;
struct Mat
{
ll a[N][N];
Mat(){memset(a,0,sizeof a);}
Mat operator*(const Mat &rhs) const
{
Mat res;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
for(int k=0;k<N;k++)
{
res.a[i][j]=(res.a[i][j]+a[i][k]*rhs.a[k][j])%mod;
}
}
}
return res;
}
Mat operator^(ll rhs) const
{
Mat res,tmp=*this;
res.a[0][0]=a2;res.a[0][1]=a1;
res.a[1][0]=0;res.a[1][1]=0;
while(rhs)
{
if(rhs&1)res=res*tmp;
tmp=tmp*tmp;
rhs>>=1;
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll p,q,n;
cin>>p>>q>>a1>>a2>>n>>mod;
if(n==1){cout<<a1<<'\n';return 0;}
if(n==2){cout<<a2<<'\n';return 0;}
Mat mat;
mat.a[0][0]=p;mat.a[0][1]=1;
mat.a[1][0]=q;mat.a[1][1]=0;
mat=mat^n-2;
cout<<mat.a[0][0]%mod<<'\n';
return 0;
}

对于 Fibonacci 数列:

fi={[i=1]i1fi1+fi2i>1f_i= \begin{cases} [i=1] & i\leq 1 \\ f_{i-1}+f_{i-2} & i\gt 1 \end{cases}

请求出 fnf_nfmf_m 的最大公约数,即 gcd(fn,fm)\gcd(f_n,f_m)

代码(1)
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int mod=100000000;
const int N=2;
struct Mat
{
ll a[N][N];
Mat(){memset(a,0,sizeof a);}
Mat operator*(const Mat &rhs) const
{
Mat res;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
for(int k=0;k<N;k++)
{
res.a[i][j]=(res.a[i][j]+a[i][k]*rhs.a[k][j])%mod;
}
}
}
return res;
}
Mat operator^(ll rhs) const
{
Mat res,tmp=*this;
for(int i=0;i<N;i++)res.a[i][i]=1;
while(rhs)
{
if(rhs&1)res=res*tmp;
tmp=tmp*tmp;
rhs>>=1;
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,m;
cin>>n>>m;
Mat mat;
mat.a[0][0]=1;mat.a[0][1]=1;
mat.a[1][0]=1;mat.a[1][1]=0;
mat=mat^__gcd(n,m)-1;
cout<<mat.a[0][0]<<'\n';
return 0;
}