矩阵
参考资料
例题
给定 的矩阵 ,求 。代码(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;
}
已知一个数列 ,它满足:
求 数列的第 项对 取余的值。代码(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;
}
大家都知道,斐波那契数列是满足如下性质的一个数列:
请你求出 的值。代码(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;
}
广义的斐波那契数列是指形如 的数列。
今给定数列的两系数 和 ,以及数列的最前两项 和 ,另给出两个整数 和 ,试求数列的第 项 对 取模后的结果。代码(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 数列:
请求出 与 的最大公约数,即 。代码(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;
}