4000: [TJOI2015]棋盘
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 500 Solved: 226
[ Submit][ Status][ Discuss]
Description
Input
输入数据的第一行为两个整数N,M表示棋盘大小。第二行为两个整数P,K,
表示攻击范围模板的大小,以及棋子在模板中的位置。接下来三行,
每行P个数,表示攻击范围的模版。每个数字后面一个空格。
Output
一个整数,表示可行方案Mod 2 ^32
Sample Input
2 2
3 1
0 1 0
1 1 1
0 1 0
3 1
0 1 0
1 1 1
0 1 0
Sample Output
7
HINT
1<=N<=10^6,1<=M<=6
Source
考虑每一行棋子的摆放,因为棋子的攻击范围只有三行
所以每行的棋子摆放受上一行的制约并影响下一行
列数小,不妨就用二进制状态表示每一行的棋子摆放情况
f[i][o]:第i行的棋子摆放状态为o的方案数
暴力枚举哪些状态之间可以转移,用矩阵乘法优化即可
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = (1 << 6);
typedef unsigned int u32;
int n,m,p,k,M,s[20][20];
bool bo[2][20];
u32 Ans;
struct data{
u32 a[N][N]; data(){memset(a,0,sizeof(a));}
data operator * (const data &B) const
{
data c;
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
for (int k = 0; k < M; k++)
c.a[i][j] += a[i][k] * B.a[k][j];
return c;
}
}D;
void Mark(int x,int y)
{
int dx = 1 - x,dy = k - y;
for (int i = 0; i < 2; i++)
for (int j = 0; j < m; j++)
{
if (i == x && j == y) continue;
int xx = i + dx,yy = j + dy;
if (xx < 0 || xx == 3 || yy < 0 || yy == p) continue;
if (s[xx][yy]) bo[i][j] = 1;
}
}
bool Judge(int x,int y)
{
memset(bo,0,sizeof(bo));
for (int i = 0; i < m; i++)
if (x & (1 << i)) Mark(0,i);
for (int i = 0; i < m; i++)
if (y & (1 << i)) Mark(1,i);
for (int i = 0; i < m; i++)
{
if ((x & (1 << i)) && bo[0][i]) return 0;
if ((y & (1 << i)) && bo[1][i]) return 0;
}
return 1;
}
data ksm(data x,int y)
{
data ret;
for (int i = 0; i < M; i++) ret.a[i][i] = 1;
for (; y; y >>= 1)
{
if (y & 1) ret = ret * x;
x = x * x;
}
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m >> p >> k; M = (1 << m);
for (int i = 0; i < 3; i++)
for (int j = 0; j < p; j++) scanf("%d",&s[i][j]);
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
if (Judge(i,j)) D.a[i][j] = 1;
D = ksm(D,n);
for (int i = 0; i < M; i++) Ans += D.a[0][i];
cout << Ans << endl;
return 0;
}