LL n;
void mul(int f[7],int m[7][7])
{
int c[7]={0};
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
c[i]=(c[i]+(LL)f[j]*m[j][i])%mod;
memcpy(f,c,sizeof c);
}
void mulself(int m[7][7])
{
int c[7][7]={0};
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
for(int k=0;k<7;k++)
c[i][j]=(c[i][j]+(LL)m[i][k]*m[k][j])%mod;
memcpy(m,c,sizeof c);
}
int main()
{
int f[7]={6,5,1,4,2,3,1};
int m[7][7]={{0,1,1,0,0,0,0},
{1,0,0,1,0,0,0},
{0,0,0,0,1,0,0},
{0,0,0,0,0,1,0},
{2,3,0,0,0,0,0},
{0,2,0,0,0,0,0},
{5,3,0,0,0,0,1}};
cin>>n;
if(n == 1)
cout<<2<<endl<<3<<endl;
else if(n == 2)
cout<<1<<endl<<4<<endl;
else if(n == 3)
cout<<6<<endl<<5<<endl;
else
{
n-=3;
while(n)
{
if(n & 1) mul(f,m);
mulself(m);
n>>=1;
}
cout<<f[0]<<endl<<f[1]<<endl;
}
//system("pause");
return 0;
}
[蓝桥杯][算法提高VIP]递推求值
\[[F(n,1),F(n,2),F(n-1,1),F(n-1,2),F(n-2,1),F(n-2,2),1]=\\
[F(n-1,1),F(n-1,2),F(n-2,1),F(n-2,2),F(n-3,1),F(n-3,2),1]
\begin{bmatrix}
0 & 1 & 1 & 0 & 0 & 0 & 0\\
1 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
2 & 3 & 0 & 0 & 0 & 0 & 0\\
0 & 2 & 0 & 0 & 0 & 0 & 0\\
5 & 3 & 0 & 0 & 0 & 0 & 1\\
\end{bmatrix}
\\
=[F(3,1),F(3,2),F(2,1),F(2,2),F(1,1),F(1,2),1]\begin{bmatrix}
0 & 1 & 1 & 0 & 0 & 0 & 0\\
1 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0\\
2 & 3 & 0 & 0 & 0 & 0 & 0\\
0 & 2 & 0 & 0 & 0 & 0 & 0\\
5 & 3 & 0 & 0 & 0 & 0 & 1\\
\end{bmatrix}^{n-3}
\]