题解:AT_abc407_d [ABC407D] Domino Covering XOR
xiaoyin2011 · · 题解
AT_abc407_d [ABC407D] 多米诺覆盖异或 题解
题目链接
题目大意
给定
思路及实现
异或和最大值显然不容易预处理,发现
用一个掩码
统计答案,这里可以预处理从掩码对应的异或和。只需要求得掩码的补集的异或和即可。
最后套一个记忆化,时空复杂度
AC Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll H, W, B[25];
ll cxs[1050005];
ll memo[25][1050005];
ll dfs(int pos, ll mask)
{
if (pos == H * W)
return cxs[(1 << (H * W)) - 1] ^ cxs[mask];
if (memo[pos][mask] != -1)
return memo[pos][mask];
ll res = dfs(pos + 1, mask);
if (mask & (1 << pos))
return memo[pos][mask] = res;
if (pos % W + 1 <= W - 1 && !(mask & (1 << (pos + 1))))
{
ll new_mask = mask | (1 << pos) | (1 << (pos + 1));
res = max(res, dfs(pos + 1, new_mask));
}
if (pos / W + 1 <= H - 1 && !(mask & (1 << (pos + W))))
{
ll new_mask = mask | (1 << pos) | (1 << (pos + W));
res = max(res, dfs(pos + 1, new_mask));
}
return memo[pos][mask] = res;
}
int main()
{
scanf("%lld%lld", &H, &W);
for (int i = 0; i <= H - 1; i++)
for (int j = 0; j <= W - 1; j++)
scanf("%lld", &B[i * W + j]);
for (int mask = 0; mask <= (1 << (H * W)) - 1; mask++)
for (int k = 0; k <= H * W - 1; k++)
if (mask & (1 << k))
cxs[mask] ^= B[k];
for (int i = 0; i <= H * W - 1; i++)
for (int j = 0; j <= (1 << (H * W)) - 1; j++)
memo[i][j] = -1;
printf("%lld\n", dfs(0, 0));
return 0;
}