POJ 3734 Blocks DP优化
来源:互联网 发布:西安网络电视在线直播 编辑:程序博客网 时间:2024/05/16 09:55
题目:http://poj.org/problem?id=3734
分析
设 f[i][1] 为第 i 位使红绿色块总数都为偶的方案
f[i][2] …… 红奇绿偶
f[i][3] …… 红偶绿奇
f[i][4] …… 红奇绿奇
则归纳可得,对于相邻的两位 i, j (i < j)
有
j1=i1∗2+i2∗1+i3∗1+i4∗0
j2=i1∗1+i2∗2+i3∗0+i4∗1
j3=i1∗1+i2∗0+i3∗2+i4∗1
j4=i1∗0+i2∗1+i3∗1+i4∗2
显然可以用矩阵乘法,详见百度
其实是博主也不太会分析
为避免 TLE ,需要使用快速幂
代码片
#include<cstdio>#include<cstring>#include<algorithm>const int M = 10007;using namespace std;int x[4][4] = {{2, 1, 1, 0}, {1, 2, 0, 1}, {1, 0, 2, 1}, {0, 1, 1, 2}};struct Matrix { int h, w; int k[4][4]; Matrix() { h = 4, w = 4; memset(k, 0, sizeof(k)); }};Matrix cal(Matrix a, Matrix b) { Matrix c; for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) for(int l = 0; l < 4; l++) c.k[i][j] += a.k[i][l] * b.k[l][j], c.k[i][j] %= M; return c;}Matrix work(Matrix f, int n) { Matrix x = f; if(n == 1) return f; f = work(f, n>>1); f = cal(f, f); if(n & 1) f = cal(f, x); return f;}int main() { int t, n; scanf("%d", &t); while(t--) { Matrix f; memcpy(f.k, x, sizeof(x)); scanf("%d", &n); f = work(f, n); printf("%d\n", f.k[0][0] % M); } return 0;}
0 0
- POJ 3734 Blocks DP优化
- POJ 3734 Blocks(矩阵优化+DP)
- poj 3734 Blocks 矩阵乘法优化dp
- Poj 3734 Blocks(DP,矩阵乘法优化)
- 3734 Blocks DP矩阵优化入门
- poj 1390 Blocks(dp)
- POJ 1390 Blocks dp
- poj 1390 Blocks(DP)
- Blocks - POJ 1390 dp
- POJ 1390 Blocks(DP)
- POJ 1390 Blocks DP *
- POJ 1390 Blocks (DP)
- poj 1390 Blocks 【区间dp】
- POJ 3734 Blocks(dp、矩阵快速幂)
- poj 3734 Blocks 递推+矩阵幂优化
- poj 3734 Blocks 矩阵
- POJ-3734-Blocks
- POJ 3734 Blocks
- Android Studio中引入.so文件
- 关于Ubuntu下idea/JetBrains系列无法切换中文输入法,Fcitx,输入中文
- CentOS上如何把Web服务器从Apache换到Nginx
- 牛比的模板跟踪
- Java使用Debug进行程序调试
- POJ 3734 Blocks DP优化
- ORB-SLAM之Relocalization,SLAM重定位流程
- 初识java,慢慢中毒
- Spring开发包介绍
- python遍历一个目录,输出所有的文件名
- 不正经运维狗文档2-2
- 网站的有效浏览时间
- Arduino UNO R3控制风扇小实验
- pyhton的部分内建函数