1. 程式人生 > >HDU 6198 矩陣快速冪

HDU 6198 矩陣快速冪

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;
}