您的位置:首页 > 其它

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

2015-05-28 19:39 423 查看

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

Sample Input

2
1
2
Sample Output

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

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <map>
# include <cmath>
# 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 ()
{
// freopen("in.txt","r",stdin) ;
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]%MOD<<endl ;

}

return 0 ;
}
View Code  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: