N个方块排成一列 用红,蓝,绿,黄4种颜色去涂色,求红色方块 和绿色方块个数同时为偶数的 方案数 对10007取余
Sample Input
2
1
2
Sample Output
2//(蓝,黄)
6//(红红,蓝蓝,蓝黄,绿绿,黄蓝,黄黄)
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <map> 6 # include <cmath> 7 # define LL long long 8 using namespace std ; 9 10 const int MOD = 10007 ; 11 12 struct Matrix 13 { 14 LL mat[3][3]; 15 }; 16 17 Matrix mul(Matrix a,Matrix b) //矩阵乘法 18 { 19 Matrix c; 20 for(int i=0;i<3;i++) 21 for(int j=0;j<3;j++) 22 { 23 c.mat[i][j]=0; 24 for(int k=0;k<3;k++) 25 { 26 c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD; 27 } 28 } 29 return c; 30 } 31 Matrix pow_M(Matrix a,int k) //矩阵快速幂 32 { 33 Matrix ans; 34 memset(ans.mat,0,sizeof(ans.mat)); 35 for (int i=0;i<3;i++) 36 ans.mat[i][i]=1; 37 Matrix temp=a; 38 while(k) 39 { 40 if(k&1)ans=mul(ans,temp); 41 temp=mul(temp,temp); 42 k>>=1; 43 } 44 return ans; 45 } 46 47 48 49 int main () 50 { 51 // freopen("in.txt","r",stdin) ; 52 int T; 53 cin>>T ; 54 Matrix t ; 55 t.mat[0][0] = 2 ; t.mat[0][1] = 1 ; t.mat[0][2] = 0 ; 56 t.mat[1][0] = 2 ; t.mat[1][1] = 2 ; t.mat[1][2] = 2 ; 57 t.mat[2][0] = 0 ; t.mat[2][1] = 1 ; t.mat[2][2] = 2 ; 58 while(T--) 59 { 60 int n ; 61 cin>>n ; 62 Matrix ans = pow_M(t,n) ; 63 cout<<ans.mat[0][0]%MOD<<endl ; 64 65 } 66 67 68 return 0 ; 69 }
时间: 2024-11-06 20:48:12