#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=9;
struct Matrix{///矩阵结构体
ll matrix[N][N];
};
const int mod = 1e9 + 7;
void init(Matrix &res)///初始化为单位矩阵(可按要求修改)
{
memset(res.matrix,0,sizeof(res.matrix));
for(int i=0;i<N;i++)
res.matrix[i][i]=1;
}
Matrix multiplicative(Matrix a,Matrix b)///矩阵乘法
{
Matrix res;
memset(res.matrix,0,sizeof(res.matrix));
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j < N ; j++){
for(int k = 0 ; k < N ; k++){
res.matrix[i][j] += a.matrix[i][k]*b.matrix[k][j];
res.matrix[i][j] %= mod;
}
}
}
return res;
}
Matrix Pow(Matrix mx,ll m)///矩阵快速幂
{
Matrix res,base=mx;//base是用来转化的关系矩阵 ,res是结果矩阵和初值矩阵???
init(res);
while(m)
{
if(m&1)
res=multiplicative(res,base);
base=multiplicative(base,base);
m>>=1;
}
return res;
}
使用实例:
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
Sample Input
2
3 1 2
4 1 10
Sample Output
85
369
Hint
In the first case, the third number is 85 = 21十2十3^4.
In the second case, the third number is 93 = 21十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=7;//定义大小
ll n,mi=1,a,b;
struct Matrix{///矩阵结构体
ll matrix[N][N];
};
const ll mod = 2147493647;//定义mod
void init(Matrix &res)///初始化为单位矩阵 (可按要求修改)
{
memset(res.matrix,0,sizeof(res.matrix));
//for(int i=0;i<N;i++)
//res.matrix[]={b,a,16,8,4,2,1};
res.matrix[0][0]=b;
res.matrix[0][1]=a;
res.matrix[0][2]=16;
res.matrix[0][3]=8;
res.matrix[0][4]=4;
res.matrix[0][5]=2;
res.matrix[0][6]=1; //初始化矩阵
}
Matrix multiplicative(Matrix a,Matrix b)///矩阵乘法
{
Matrix res;
memset(res.matrix,0,sizeof(res.matrix));
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j < N ; j++){
for(int k = 0 ; k < N ; k++){
res.matrix[i][j] += a.matrix[i][k]*b.matrix[k][j];
res.matrix[i][j] %= mod;
}
}
}
return res;
}
Matrix Pow(Matrix mx,ll m)///矩阵快速幂
{
Matrix res,base=mx;//base是用来转化的关系矩阵 ,res是结果矩阵和初值矩阵???
init(res);
while(m)
{
if(m&1)
res=multiplicative(res,base);
base=multiplicative(base,base);
m>>=1;
}
return res;
}
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;++i)
{
cin>>n>>a>>b;
Matrix m={1,1,0,0,0,0,0,2,0,0,0,0,0,0,1,0,1,0,0,0,0,4,0,4,1,0,0,0,6,0,6,3,1,0,0,4,0,4,3,2,1,0,1,0,1,1,1,1,1};//初始化转化关系矩阵
cout<<Pow(m,n-2).matrix[0][0]<<endl;//输出答案,n-2指初值矩阵经过了n-2次与转化关系矩阵的乘法,n-2要自己计算出来。
}
return 0;
}
矩阵快速幂的工作过程(以斐波那契数列为例):
下面是各种转换矩阵的构造方法:
来个洛谷的模板题(luogu P3390):
没啥好说的,转换矩阵等于初始矩阵,改改模板即可,注意数据都要开long long,下面是代码:
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=105;//矩阵大小
struct Matrix{///矩阵结构体
ll matrix[N][N];
};
ll n,k;//要开long long
const int mod = 1e9 + 7;//模
void init(Matrix &res)///初始化为单位矩阵(可按要求修改)
{
memset(res.matrix,0,sizeof(res.matrix));
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
cin>>res.matrix[i][j];//初始矩阵初始化
}
}
}
Matrix multiplicative(Matrix a,Matrix b)///矩阵乘法
{
Matrix res;
memset(res.matrix,0,sizeof(res.matrix));
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j < N ; j++){
for(int k = 0 ; k < N ; k++){
res.matrix[i][j] += a.matrix[i][k]*b.matrix[k][j];
res.matrix[i][j] %= mod;
}
}
}
return res;
}
Matrix Pow(ll m)///矩阵快速幂
{
Matrix res,base;//base是用来转化的关系矩阵 ,res是结果矩阵和初值矩阵???
init(res);
base=res;//这题中转换矩阵等于初始矩阵
while(m)
{
if(m&1)
res=multiplicative(res,base);
base=multiplicative(base,base);
m>>=1;
}
return res;
}
int main()
{
cin>>n>>k;
Matrix a;
a=Pow(k-1);//往里面传的参数是要进行运算的次数,k次方是运算k-1次,所以往里面传的是k-1。
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
{
cout<<a.matrix[i][j]<<" ";
}
cout<<endl;
}
return 0;
}