题目
给你
n
×
m
n\times m
n×m的方格,在里面放棋子,并给出
3
×
p
3\times p
3×p矩阵作为棋子攻击范围
求方案数
题解
首先!吐槽出题人!为什么没有说起始是第 0 0 0行第 0 0 0列!,所以棋子在攻击范围的第二行,直接0分,调了几个小时
一眼看数据范围,肯定是状压没跑了
冷静分析一波,状态数64,对当前状态有影响的只有上一行,所以
f
[
n
]
[
s
0
]
[
s
1
]
f[n][s0][s1]
f[n][s0][s1]三维数组,用回滚数组滚掉第一维
技巧
通过观察发现我们的n次转移过程完全一样,于是我们可以将转移过程变成矩阵乘法,用矩阵快速幂来加速
#include<bits/stdc++.h>
using namespace std;
int n,m,p,k;
struct nd{int x,y;}bt[1000];int bs;
int z[100],g;
struct Mat{
unsigned int t[100][100];
Mat(){memset(t,0,sizeof(t));}
friend Mat operator*(const Mat a,const Mat b)
{
Mat ans;
for(int i=1;i<=g;i++)
for(int j=1;j<=g;j++)
for(int k=1;k<=g;k++)
ans.t[i][j]+=a.t[i][k]*b.t[k][j];
return ans;
}
};
bool check(int s0,int s1)
{
for(int i=0;i<m;i++)
{
if(s0&(1<<i))
for(int w=1;w<=bs;w++)
{
int j=i+bt[w].y;if(j>=m)continue;
if(bt[w].x==0){if(s0&(1<<j))return 0;}
else if(bt[w].x==1){if(s1&(1<<j))return 0;}
}
if(s1&(1<<i))
for(int w=1;w<=bs;w++)
{
int j=i+bt[w].y;if(j>=m)continue;
if(bt[w].x==0){if(s1&(1<<j))return 0;}
else if(bt[w].x==-1){if(s0&(1<<j))return 0;}
}
}
}
Mat f,a,ans;
int main()
{
scanf("%d%d%d%d",&n,&m,&p,&k);
for(int i=0;i<3;i++)
for(int j=0;j<p;j++)
{
int fw;
scanf("%d",&fw);
if(fw==1&&(i!=1||j!=k))bt[++bs]={i-1,j-k};
}
a.t[1][1]=1;
for(int s0=0;s0<(1<<m);s0++)if(check(s0,0))z[++g]=s0;
for(int i=1;i<=g;i++)for(int j=1;j<=g;j++)if(check(z[i],z[j]))f.t[i][j]=1;
for(int i=1;i<=g;i++)ans.t[i][i]=1;
while(n)
{
if(n&1)ans=ans*f;
f=f*f;
n>>=1;
}
ans=a*ans;
unsigned int tmp=0;
for(int i=1;i<=g;i++)tmp+=ans.t[1][i];
cout<<tmp;
}