Poj 3734 Blocks(DP,矩阵乘法优化)
2015-03-20 21:24
351 查看
题目链接:poj 3734
这道题用矩阵乘法优化DP。
考虑到直接转移的话,N太大,会TLE。由于转移的方案数,转移的状态很少,所以可以将转移的方案用矩阵来表示。
设dp[i][k]表示涂到第i个格子,状态为k的方案数。其中状态k的定义为:
k=1:红色和绿色都为奇数,k=2:红奇绿偶,k=3:红偶绿奇,k=4:红偶绿偶
DP方程自己写。
表示为矩阵为:
2 1 1 0
1 2 0 1
1 0 2 1
0 1 1 2
然后做矩阵快速幂即可。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define mod (10007)
struct node{
int a[10][10];
node(){
memset(a,0,sizeof(a));
}
}S;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
node mut(node x,node y){
node t;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
for(int p=1;p<=4;p++)
t.a[i][j]=(t.a[i][j]+x.a[i][p]*y.a[p][j])%mod;
return t;
}
node pow(node x,int y){
node t;
for(int i=1;i<=4;i++)t.a[i][i]=1;
while(y){
if(y&1)t=mut(t,x);
y>>=1;
x=mut(x,x);
}
return t;
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
S.a[1][1]=2; S.a[1][2]=1; S.a[1][3]=1; S.a[1][4]=0;
S.a[2][1]=1; S.a[2][2]=2; S.a[2][3]=0; S.a[2][4]=1;
S.a[3][1]=1; S.a[3][2]=0; S.a[3][3]=2; S.a[3][4]=1;
S.a[4][1]=0; S.a[4][2]=1; S.a[4][3]=1; S.a[4][4]=2;
int T=read();
while(T--){
int N=read();
node ans=pow(S,N);
printf("%d\n",ans.a[4][4]);
}
return 0;
}
这道题用矩阵乘法优化DP。
考虑到直接转移的话,N太大,会TLE。由于转移的方案数,转移的状态很少,所以可以将转移的方案用矩阵来表示。
设dp[i][k]表示涂到第i个格子,状态为k的方案数。其中状态k的定义为:
k=1:红色和绿色都为奇数,k=2:红奇绿偶,k=3:红偶绿奇,k=4:红偶绿偶
DP方程自己写。
表示为矩阵为:
2 1 1 0
1 2 0 1
1 0 2 1
0 1 1 2
然后做矩阵快速幂即可。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define mod (10007)
struct node{
int a[10][10];
node(){
memset(a,0,sizeof(a));
}
}S;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
node mut(node x,node y){
node t;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
for(int p=1;p<=4;p++)
t.a[i][j]=(t.a[i][j]+x.a[i][p]*y.a[p][j])%mod;
return t;
}
node pow(node x,int y){
node t;
for(int i=1;i<=4;i++)t.a[i][i]=1;
while(y){
if(y&1)t=mut(t,x);
y>>=1;
x=mut(x,x);
}
return t;
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
S.a[1][1]=2; S.a[1][2]=1; S.a[1][3]=1; S.a[1][4]=0;
S.a[2][1]=1; S.a[2][2]=2; S.a[2][3]=0; S.a[2][4]=1;
S.a[3][1]=1; S.a[3][2]=0; S.a[3][3]=2; S.a[3][4]=1;
S.a[4][1]=0; S.a[4][2]=1; S.a[4][3]=1; S.a[4][4]=2;
int T=read();
while(T--){
int N=read();
node ans=pow(S,N);
printf("%d\n",ans.a[4][4]);
}
return 0;
}
相关文章推荐
- POJ 3734 Blocks(矩阵优化+DP)
- POJ 3734 Blocks【用母函数推公式|矩阵乘法】
- poj 3734 Blocks (矩阵快速幂优化的动态规划)
- poj 3734 Blocks 矩阵乘法优化dp
- POJ 3734 Blocks(dp、矩阵快速幂)
- POJ 3734 Blocks DP优化
- Blocks - POJ 3734 矩阵乘法递推
- POJ 3744 分段矩阵乘法优化求概率
- POJ 3734 —— 矩阵快速幂 + DP
- POJ 3734 Blocks 线性递推 矩阵快速幂
- POJ 3233 - 矩阵乘法及其性质和优化
- poj3734--blocks(矩阵快速幂)
- poj 3744 Scout YYF I(矩阵优化概率DP)
- [POJ 3734] Blocks (矩阵快速幂、组合数学)
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
- BZOJ 1875 [SDOI 2009] HH去散步 (DP,矩阵乘法优化)
- BZOJ 1875 SDOI 2009 HH去散步 矩阵乘法优化DP
- POJ 3735 矩阵乘法的小优化GET。
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
- poj 3744 Scout YYF I(概率dp,矩阵优化)