版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuxiyulu/article/details/78219233
/*
递推公式+矩阵快速幂
a(n)=a(n-1)+5*a(n-2)+a(n-3)-a(n-4)
*/
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int mod=1e9+7;
LL n;
struct Matrix
{
LL mat[4][4];
Matrix()//注意初始化
{
memset(mat,0,sizeof(mat));
}
Matrix operator -(Matrix &tmp)
{
Matrix res;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
{
res.mat[i][j]=mat[i][j]-tmp.mat[i][j]+mod;
res.mat[i][j]%=mod;
}
return res;
}
Matrix operator *(Matrix &tmp)
{
Matrix res;
memset(res.mat,0,sizeof(res.mat));
for(int i=0; i<4; ++i)
for(int j=0; j<4; ++j)
for(int k=0; k<4; ++k)
{
res.mat[i][j]+=mat[i][k]*tmp.mat[k][j];
res.mat[i][j]%=mod;
}
return res;
}
};
Matrix pow_M(Matrix a,LL n)
{
Matrix ret;
memset(ret.mat,0,sizeof(ret.mat));
ret.mat[0][0]=ret.mat[1][1]=1;
Matrix temp=a;
while(n)
{
if(n&1)ret=ret*temp;
temp=temp*temp;
n>>=1;
}
return ret;
}
LL pow_m(LL a,LL n)
{
LL ret=1;
LL temp=a%mod;
while(n)
{
if(n&1)
{
ret*=temp;
ret%=mod;
}
temp*=temp;
temp%=mod;
n>>=1;
}
return ret;
}
int main()
{
while(~scanf("%I64d",&n))
{
Matrix A,B,C;
//A为系数矩阵,B为基数矩阵
A.mat[0][0]=1;
A.mat[0][1]=5;
A.mat[0][2]=1;
A.mat[0][3]=-1;
A.mat[1][0]=1;
A.mat[2][1]=1;
A.mat[3][2]=1;
B.mat[0][0]=36;
B.mat[1][0]=11;
B.mat[2][0]=5;
B.mat[3][0]=1;
if(n==1)
{
printf("1\n");
continue;
}
if(n==2)
{
printf("5\n");
continue;
}
if(n==3)
{
printf("11\n");
continue;
}
if(n==4)
{
printf("36\n");
continue;
}
C=pow_M(A,n-4)*B;
printf("%I64d\n",(C.mat[0][0]+mod)%mod);
}
return 0;
}
/*
状态压缩dp+矩阵快速幂
*/
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int mod=1e9+7;
LL n;
struct Matrix
{
LL mat[6][6];
Matrix(){memset(mat,0,sizeof(mat));}//注意初始化
Matrix operator -(Matrix &tmp)
{
Matrix res;
for(int i=0; i<6; i++)
for(int j=0; j<6; j++)
{
res.mat[i][j]=mat[i][j]-tmp.mat[i][j]+mod;
res.mat[i][j]%=mod;
}
return res;
}
Matrix operator *(Matrix &tmp)
{
Matrix res;
memset(res.mat,0,sizeof(res.mat));
for(int i=0; i<6; ++i)
for(int j=0; j<6; ++j)
for(int k=0; k<6; ++k)
{
res.mat[i][j]+=mat[i][k]*tmp.mat[k][j];
res.mat[i][j]%=mod;
}
return res;
}
};
Matrix pow_M(Matrix a,LL n)
{
Matrix ret;
memset(ret.mat,0,sizeof(ret.mat));
ret.mat[0][0]=ret.mat[1][1]=1;
Matrix temp=a;
while(n)
{
if(n&1)ret=ret*temp;
temp=temp*temp;
n>>=1;
}
return ret;
}
int main()
{
while(~scanf("%I64d",&n))
{
Matrix A,B;
/*
状态可达矩阵,根据某一行状态转移到下一行时,可达的状态
每一行分成4个部分,已填用1表示,未填用0表示
下一行 0000 0011 0110 1001 1100 1111
某一行 0 1 2 3 4 5
0000 0 1 1 1 1 1
0011 1 1 1
0110 2 1
1001 3 1 1
1100 4 1 1
1111 5 1
某一行与下一行结合后符合实际填补的情况
*/
//初始化状态可达矩阵
A.mat[0][0]=1;
A.mat[0][1]=1;
A.mat[0][3]=1;
A.mat[0][4]=1;
A.mat[0][5]=1;
A.mat[1][0]=1;
A.mat[1][4]=1;
A.mat[2][3]=1;
A.mat[3][0]=1;
A.mat[3][2]=1;
A.mat[4][0]=1;
A.mat[4][1]=1;
A.mat[5][0]=1;
B=pow_M(A,n);
//恰好填满时,从状态0000->0000
printf("%I64d\n",(B.mat[0][0])%mod);
}
return 0;
}