P1896: [SCOI2005] Mutual Non-Aggression — Solution
Approach
This is a bitmask DP problem. I had never learned bitmask DP before this; this morning, with the explanation from the big shot lornd, I came to understand bitmask DP. However, my ability is limited, so I can't sum up this slick trick of bitmask DP in general terms. The essence of bitmask DP lies in merging multiple states into a binary number and then doing subset generation. Here we design a state, and this state encodes how the pieces are placed in the current row. For example, 01110 means pieces are placed in the middle three positions; for simplicity, we can compress this state directly into the decimal number 14. This is state compression.
I strongly recommend that readers learn the basics of bitwise operations before reading articles on state compression; that way, understanding it will take half the effort for twice the result.
Next, once we've figured out how to compress states, we do the DP. First we need to do some preprocessing, and the goals of the preprocessing are: compute the single-row states, compute the states between two rows, and compute the number of pieces in each state.
Computing the single-row states and the piece counts is fairly simple: enumerate states in [0,2^32) and check whether there are two adjacent 1s. If there are, set it to false in the state table, meaning this state can't be used, i.e. it can't be included in the computation. The code is as follows:
for (int i = 0; i < MAX; i++)
{
// check if there are pairs of 1;
if (i & (i >> 1))
continue;
// checked and set it true;
sigLine[i] = true;
// get the number of 1 within the state and record them;
for (int j = 1; j <= i; j <<= 1)
if (j & i)
king[i]++;
}
Here we set MAX = 1 << n, i.e. the upper bound of the states. As for why it's less than MAX rather than less than or equal to MAX, readers can think about whether there is any difference between the two.
Next we need to compute the states between two rows. We open an array called \(doubleLine[1<<maxn][1<<maxn]\), where if \(doubleLine[x][y]==true\), it means the first row having state x and the second row having state y can exist, and can be included in the computation. That way, it's not hard to come up with a brute-force way to do the preprocessing:
// the first line;
for (int i = 0; i < MAX; i++)
if (sigLine[i])
// the second line;
for (int j = 0; j < MAX; j++)
// check if these two are valid.
if (sigLine[j] && !(i & j) && !(i & (j << 1)) && !(i & (j >> 1)))
// set!
doubleLine[i][j] = true;
Our preprocessing is about done, so next we write the recurrence of the dynamic programming. First let's design the equation; it's not hard to come up with the equation:
\(dp[前n行数][累计w个棋子][本行的状态] = 当前状态数\)
\(dp[i+1][w+king[next]][nextLine] = dp[i][w][currentLine]\)
Finally, we enumerate all the states on row n with k pieces, and summing them up gives the answer. Pay special attention: this problem needs the range of long long, so watch out for overflow.
Code
// P1896.cpp
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 11;
#define ll long long
int MAX;
int n, k;
bool sigLine[1 << maxn];
bool doubleLine[1 << maxn][1 << maxn];
int king[1 << maxn];
ll dp[maxn][100][1 << maxn];
int main()
{
cin >> n >> k;
MAX = 1 << n;
for (int i = 0; i < MAX; i++)
{
// check if there are pairs of 1;
if (i & (i >> 1))
continue;
// checked and set it true;
sigLine[i] = true;
// get the number of 1 within the state and record them;
for (int j = 1; j <= i; j <<= 1)
if (j & i)
king[i]++;
}
// the first line;
for (int i = 0; i < MAX; i++)
if (sigLine[i])
// the second line;
for (int j = 0; j < MAX; j++)
// check if these two are valid.
if (sigLine[j] && !(i & j) && !(i & (j << 1)) && !(i & (j >> 1)))
// set!
doubleLine[i][j] = true;
for (int i = 0; i <= MAX; i++)
if (sigLine[i])
dp[1][king[i]][i] = 1;
for (int i = 1; i < n; i++)
for (int poss = 0; poss < MAX; poss++)
if (sigLine[poss])
for (int nextLine = 0; nextLine < MAX; nextLine++)
if (sigLine[nextLine] && doubleLine[poss][nextLine])
for (int w = king[poss]; w + king[nextLine] <= k; w++)
dp[i + 1][w + king[nextLine]][nextLine] += dp[i][w][poss];
ll ans = 0;
for (int i = 0; i < MAX; i++)
ans += dp[n][k][i];
cout << ans;
return 0;
}