矩阵快速幂模板+原理+转换矩阵的构造方法

#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 = 2
1十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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值