菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
27
0

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

原创
05/13 14:22
阅读数 76222

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 }
View Code

 

发表评论

0/200
27 点赞
0 评论
收藏