Description
衣食无忧的 Q老师 有一天突发奇想,想要去感受一下劳动人民的艰苦生活。
具体工作是这样的,有 N 块砖排成一排染色,每一块砖需要涂上红、蓝、绿、黄这 4 种颜色中的其中 1 种。且当这 N 块砖中红色和绿色的块数均为偶数时,染色效果最佳。
为了使工作效率更高,Q老师 想要知道一共有多少种方案可以使染色效果最佳,你能帮帮他吗?
Input
第一行为 T,代表数据组数。(1 ≤ T ≤ 100)
接下来 T 行每行包括一个数字 N,代表有 N 块砖。(1 ≤ N ≤ 1e9)
Output
输出满足条件的方案数,答案模 10007。
Sample Input
2
1
2
Sample Output
2
6
解题思路:
#include <iostream>
#include <cstring>
# define LL long long
using namespace std ;
const int MOD = 10007 ;
struct Matrix
{
LL mat[3][3];
};
Matrix mul(Matrix a,Matrix b) //矩阵乘法
{
Matrix c;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
c.mat[i][j]=0;
for(int k=0;k<3;k++)
{
c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD;
}
}
return c;
}
Matrix pow_M(Matrix a,int k) //矩阵快速幂
{
Matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for (int i=0;i<3;i++)
ans.mat[i][i]=1;
Matrix temp=a;
while(k)
{
if(k&1)ans=mul(ans,temp);
temp=mul(temp,temp);
k>>=1;
}
return ans;
}
int main ()
{
int T;
cin>>T ;
Matrix t ;
t.mat[0][0] = 2 ; t.mat[0][1] = 1 ; t.mat[0][2] = 0 ;
t.mat[1][0] = 2 ; t.mat[1][1] = 2 ; t.mat[1][2] = 2 ;
t.mat[2][0] = 0 ; t.mat[2][1] = 1 ; t.mat[2][2] = 2 ;
while(T--)
{
int n ;
cin>>n ;
Matrix ans = pow_M(t,n) ;
cout<<ans.mat[0][0]<<endl ;
}
return 0 ;
}