HDU 6198 矩阵快速幂

本文介绍了一种利用矩阵快速幂解决特定数列问题的方法,通过定义特定的矩阵运算及快速幂技巧,高效地计算出数列的第n项值。代码实现了矩阵加法、乘法、取模操作,并通过快速幂算法加速计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ans=F(2*i+3)-1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100005;
const ll mod=998244353;
const int maxm=2;
typedef struct Node{
	int f[maxm][maxm];
	Node(int a=0,int b=0,int c=0,int d=0){
		f[0][0]=a,f[0][1]=b;
		f[1][0]=c,f[1][1]=d;
	}
	Node operator + (const Node &x) const{
		Node ans;
		for(int i=0;i<maxm;i++){
			for(int j=0;j<maxm;j++){
				ans.f[i][j]=(f[i][j]+x.f[i][j])%mod;
			}
		}
		return ans;
	}
	Node operator * (const Node &x) const{
		Node ans;
		for(int i=0;i<maxm;i++){
			for(int j=0;j<maxm;j++){
				ll temp=0;
				for(int k=0;k<maxm;k++){
					temp=(temp+((f[i][k]%mod)*(x.f[k][j]%mod)%mod))%mod;
				}
				ans.f[i][j]=temp;
			}
		}
		return ans;
	}
	Node operator % (const ll &mod){
		for(int i=0;i<maxm;i++){
			for(int j=0;j<maxm;j++){
				f[i][j]%=mod;
			}
		}
		return *this;
	}
}node;

node quick_pow(node a,ll b,ll mod){
	node ans(1,0,1,0);
	while(b){
		if(b&1LL){
			ans=(ans*a)%mod;
		}
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	ll n;
	node temp(1,1,1,0);
	while(cin>>n){
		node ans=quick_pow(temp,2*n+2,mod);
		cout<<(ans.f[0][0]-1+mod)%mod<<endl;
	}	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值