poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数 (矩阵快速幂)
2015-05-28 19:39
519 查看
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
相关文章推荐
- Linux 输入\输出相关命令 echo read tee
- Android-AlarmManager多个闹钟相互独立的实现
- MFCC特征提取(C语言版本)
- iOS绘图-线条旋转锯齿问题
- 第11周项目4-3求过圆外一点与圆心的连线与圆的两个交点
- Spark随机深林扩展—OOB错误评估和变量权重
- mysql故障:重启mysql后启动报错
- 读书笔记的四种形式
- listView_分割线
- 算法-整数的正序和逆序输出
- Debian 8.0(Jessie) 无线网卡,ATI显卡驱动和输入法等安装记录。
- maven学习(四)
- [20150428][Android第一个程序][拨号器]
- Sql 更改表名,SQL2000批量更改表所有者 的方式
- 从客户端检测到有潜在危险的Request.Form值
- STL之RTTI
- 18_ListView实现分组效果
- Java关键字final、static使用总结(精)
- 第十三周 项目四--点、圆的关系(1~4)
- NFS(Network File System) 服务的配置与应用