Bob’s school has a big playground, boys and girls always play games here after school.

To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.

Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.

He has infinite carpets with sizes of 1×2 and 2×1, and the size of the playground is 4×n.

Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
Input
There are no more than 5000 test cases.

Each test case only contains one positive integer n in a line.

1≤n≤10^18
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5

f(n)=f(n-1)+f(n-2)*5+f(n-3)-f(n-4)

然后根据递推公式用矩阵快速幂解决

值得注意的是由于矩阵系数有负数,<mark>在做矩阵相乘时注意+mod再取模</mark>

找规律题解可以看(有图解)
https://blog.csdn.net/elbadaernu/article/details/77825979
高斯消元题解(特别详细)
https://www.cnblogs.com/shuaihui520/p/10002245.html

#include<cstdio>
#include<cstring>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
struct mat {
	ll a[4][4];
};
mat operator*(mat a,mat b) {
	mat s;
	memset(s.a,0,sizeof(s.a));
	for(int i=0; i<4; i++) {
		for(int j=0; j<4; j++) {
			for(int k=0; k<4; k++) {
				s.a[i][j]=(s.a[i][j]+a.a[i][k]*b.a[k][j]+mod)%mod;
			}
		}
	}
	return s;
}
mat ksm(mat a,long long b) {
	mat s;
	for(int i=0; i<4; i++) {
		for(int j=0; j<4; j++) {
			if(i==j) s.a[i][j]=1;
			else s.a[i][j]=0;
		}
	}
	while(b) {
		if(b&1) s=s*a;
		a=a*a;
		b>>=1;
	}
	return s;
}
int main() {
	ll n;
	while(~scanf("%lld",&n)) {
		if(n==1) {
			printf("1\n");
			continue;
		} else if(n==2) {
			printf("5\n");
			continue;
		} else if(n==3) {
			printf("11\n");
			continue;
		} else if(n==4) {
			printf("36\n");
			continue;
		} else {
			mat a;
			a.a[0][0]=1,a.a[0][1]=5,a.a[0][2]=1,a.a[0][3]=-1;
			for(int i=1; i<4; i++) {
				for(int j=0; j<4; j++) {
					if(j==i-1) a.a[i][j]=1;
					else a.a[i][j]=0;
				}
			}
			mat s=ksm(a,n-4);
			ll ans=(s.a[0][0]*36%mod+s.a[0][1]*11%mod+s.a[0][2]*5%mod+s.a[0][3]*1%mod)%mod;
			printf("%lld\n",ans);
		}
	}
	return 0;
}