矩阵快速幂模板

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const int mod=1e9+7;
const int N=9;
struct node
{
	long long a[N][N];
	void init0()//零矩阵
	{
		memset(a,0,sizeof(a));
	}
	void init1()//单位矩阵
	{
		init0();
		for(int i=0;i<N;i++)
			a[i][i]=1;
	}
	void init()
	{
		init0();
		a[0][3]=1,a[0][6]=1;
		a[1][0]=1,a[1][3]=1,a[1][6]=1;
		a[2][0]=1,a[2][3]=1;
		a[3][1]=1,a[3][4]=1,a[3][7]=1;
		a[4][1]=1,a[4][7]=1;
		a[5][1]=1,a[5][4]=1;
		a[6][2]=1,a[6][8]=1;
		a[7][5]=1,a[7][8]=1;
		a[8][2]=1,a[8][5]=1;
	}
	node operator*(const node&b)const
	{
		node temp;
		temp.init0();
		for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<N;k++)
					temp.a[i][j]=(temp.a[i][j]+a[i][k]*b.a[k][j]%mod)%mod;
		return temp;
	}
};
node qpow(long long b)
{
	node ans,ju;
	ju.init();
	ans.init1();
	while(b>0)
	{
		if(b&1) ans=ans*ju;
		ju=ju*ju;
		b>>=1;
	}
	return ans;
}

posted on 2018-10-02 11:12  TRZNDP_Z  阅读(119)  评论(0编辑  收藏  举报

导航