2017江西icpc邀请赛 快速幂板子

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MD=1000000007;
int p[5]= {0,1,5,11,36};
struct Matrix {
	ll a[4][4];
	Matrix() {
		memset(a,0,sizeof(a));
	}
	Matrix operator*(const Matrix  y) {
		Matrix  ans;
		for(int i=0; i<4; i++)
			for(int j=0; j<4; j++)
				for(int k=0; k<4; k++)
					ans.a[i][j]=(ans.a[i][j]+a[i][k]*y.a[k][j]+MD)%MD;
		return ans;
	}
	void operator=(const Matrix b) {
		for(int i=0; i<4; i++)
			for(int j=0; j<4; j++)
				a[i][j]=b.a[i][j];
	}
};
ll mat_pow(ll n) {
	Matrix c,res;
	c.a[0][0]=c.a[0][1]=c.a[1][2]=c.a[2][3]=c.a[2][0]=1;
	c.a[3][0]=-1,c.a[1][0]=5;
	res.a[0][0]=36,res.a[0][1]=11,res.a[0][2]=5,res.a[0][3]=1;
	while(n) {
		if(n&1) res=res*c;
		c=c*c;
		n=n>>1;
	}
	//for(int i=0;i<4;i++){
	//	cout<<res.a[0][i]<<endl;
	//}
	return res.a[0][0];
}
int main() {
	ll n;
	while(~scanf("%lld",&n)) {
		if(n<=4) {
			printf("%d\n",p[n]);
		} else printf("%lld\n",mat_pow(n-4));
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值