在找矩阵快速幂的模板的时候,始终没有找到类似f(n)=a1f(n-1)+a2f(n-2)+…ax*f(n-x)的模板,于是总结一下这种奇奇怪怪的形式
//用于解决f(n)=a*f(n-1)+b*f(n-2)+c*f(n-3).....xjb*f(xiajiba)//共N项
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod=1e8+7;
const int maxn=1e5+10;
ll pre[maxn];
const int N=2;
struct mat{
ll m[N+1][N+1];
mat(){memset(m, 0, sizeof(m));}
mat(int a[]){memset(m, 0, sizeof(m));
for(int i=1;i<=N;i++)m[1][i]=a[i];
}
mat operator*(const mat other) const{
mat ans;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
for(int k = 1; k <= N; k++){
ans.m[i][j] = (ans.m[i][j] + this->m[i][k] * other.m[k][j]) % mod;
}
}
}
return ans;
}
mat matqpow(ll p){
mat ans;
int i, j;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(i == j) ans.m[i][j] = 1;
else ans.m[i][j] = 0;
}
}
while(p){
if(p & 1) ans = (ans**this);
*this = *this**this;
p >>= 1;
}
return ans;
}
};
int main(){
ll num;
//对于f(n)=4*f(n-1)+255*f(n-2)+f(n-3) f(1)=2,f(2)=4,f(3)=114514
int p,q,a1,a2;
cin>>num;
int re[]={0,4,255,1},xishu[]={0,2,4,114514};
mat res(re);
for(int i=2;i<=N;i++)res.m[i][i-1]=1;
mat ans;
for(int i=1;i<=N;i++)ans.m[i][1]=xishu[N+1-i];
if(num <=N){
cout<<xishu[num]%mod;
}
else {
num -= N;
res = res.matqpow(num);
res = res*ans;
cout << res.m[1][1] % mod ;
}
return 0;
}