poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数 (矩阵快速幂)

时间:2023-03-08 21:43:22

N个方块排成一列 用红,蓝,绿,黄4种颜色去涂色,求红色方块 和绿色方块个数同时为偶数的 方案数 对10007取余

Sample Input

2
1
2
Sample Output

2//(蓝,黄)
6//(红红,蓝蓝,蓝黄,绿绿,黄蓝,黄黄)

poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数   (矩阵快速幂)

poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数   (矩阵快速幂)

 # include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <map>
# include <cmath>
# define LL long long
using namespace std ; const int MOD = ; struct Matrix
{
LL mat[][];
}; Matrix mul(Matrix a,Matrix b) //矩阵乘法
{
Matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
c.mat[i][j]=;
for(int k=;k<;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,,sizeof(ans.mat));
for (int i=;i<;i++)
ans.mat[i][i]=;
Matrix temp=a;
while(k)
{
if(k&)ans=mul(ans,temp);
temp=mul(temp,temp);
k>>=;
}
return ans;
} int main ()
{
// freopen("in.txt","r",stdin) ;
int T;
cin>>T ;
Matrix t ;
t.mat[][] = ; t.mat[][] = ; t.mat[][] = ;
t.mat[][] = ; t.mat[][] = ; t.mat[][] = ;
t.mat[][] = ; t.mat[][] = ; t.mat[][] = ;
while(T--)
{
int n ;
cin>>n ;
Matrix ans = pow_M(t,n) ;
cout<<ans.mat[][]%MOD<<endl ; } return ;
}