题目链接:点击打开链接
解题思路:打表找规律发现f(n) = 4 f(n-1) + 17 f(n-2) - 12 f(n-3),然后矩阵快速幂即可
代码:
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int mx = 1e5+10,mod = 1e9+7;
ll n;
struct data{
ll mart[3][3];
}ma;
const data da = {1255,197,31,0,0,0,0,0,0},base = {4,1,0,17,0,1,-12,0,0};
data juzhen(data A,data B){
data ans;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
ll c = 0;
for(int k=0;k<3;k++)
c = (c+A.mart[i][k]*B.mart[k][j])%mod;
ans.mart[i][j] = c;
}
}
return ans;
}
ll kuaisu(ll x){
data ret = base;
while(x){
if(x&1) ma = juzhen(ma,ret);
x >>= 1;
ret = juzhen(ret,ret);
}
return (ma.mart[0][0]+mod)%mod;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
ma = da;
if(n==2) puts("31");
else if(n==3) puts("197");
else if(n==4) puts("1255");
else{
n -= 4;
printf("%lld\n",kuaisu(n));
}
}
return 0;
}