参考大佬博客链接
简单介绍
状压 dp 是动态规划的一种,通过将状态压缩为整数来达到优化转移的目的。
基础位运算
名称 | 符号 | 举例 |
---|---|---|
按位与 | a & b | 111 & 101 = 101 |
按位或 | a l b | 111 & 000 = 111 |
按位取反 | ~a | ~100 = 011 |
按位异或 | a ^ b | 100 ^ 101 = 001 |
左移 | a << b | 001 << 2 = 100 |
右移 | a >> b | 100 >> 2 = 001 |
常用技巧
- 取第n位: x >> n & 1
- 将最后一位置为0: (x | 1) - 1
- 将最后一位置为1:x | 1
- 将最后一位取反:x ^ 1
- 将第n位变为1:x | (1 << n)
- 将第n位变为0:x & (~(1 << n))
- 取最低位的1:x & -x
- 将最低位的0变为1:x | (x+1)
- 将最低位的1变为0:x & (x-1)
例题
Corn-Fields-G
转移时考虑到三种限制条件
- 种植的植物左右不能相邻(往左或往右移一位与原来的状态按位与结果为0)
- 植物只能种在肥沃的土地上(当前状态与土地状态按位与结果为当前状态)
- 种植植物上下不能相邻(枚举上一位的状态去转移)
#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
#define il inline
#define re register
const int N = 1e5 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int MOD = 1e9;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
int n, m, cnt;
ll dp[15][1 << 13];
int state[15], a[15][15], flag[1 << 13];
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
state[i] = 0;
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
state[i] += a[i][j] << (j - 1);
}
}
cnt = 1 << m;
for (int i = 0; i < cnt; i++) if (!(i & (i >> 1))) flag[i] = true;
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < cnt; j++) {
if (flag[j] && (j & state[i]) == j) {
for (int k = 0; k < cnt; k++) {
if (!(k & j)) dp[i][j] += dp[i-1][k], dp[i][j] %= MOD;
}
}
}
}
ll ans = 0;
for (int i = 0; i < cnt; i++) ans += dp[n][i], ans %= MOD;
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
solve();
}
[SCOI2005]互不侵犯
几个注意点
- 上下转移和左右转移和上题基本一致
- 上下转移时要上一行状态的左移和右移一位的状态均满足
- 需要三个状态f[i][j][k]代表第i行,当前状态j和已经用了k个棋子
#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
#define il inline
#define re register
const int N = 1e5 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int MOD = 1e9;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
int n, m, cnt;
ll dp[10][1 << 10][100];
int flag[1 << 10], num[1 << 10];
void solve() {
cin >> n >> m; cnt = 1 << n;
for (int i = 0; i < cnt; i++) {
if (!(i & (i >> 1))) flag[i] = true;
int tmp = i;
while (tmp) {
num[i] += tmp % 2;
tmp >>= 1;
}
}
for (int i = 0; i < cnt; i++) if (flag[i]) dp[1][i][num[i]] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < cnt; j++) {
if (flag[j]) {
for (int k = 0; k < cnt; k++) {
if (!(k & j) && !(k >> 1 & j) && !(k << 1 & j)) {
for (int l = m; l >= num[j]; l--) {
dp[i][j][l] += dp[i - 1][k][l - num[j]];
}
}
}
}
}
}
ll ans = 0;
for (int i = 0; i < cnt; i++) ans += dp[n][i][m];
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
solve();
}
[NOI2001]炮兵阵地
比较经典也是很有意思的题,看题意就是要保存两层的状态,但是如果都用1<<m肯定会mle,因此我们可以只保留可行的状态,即左右两边两格都满足的状态,打一下表发现不会超过70个。
- 注意初始化的方式
- 注意前后两层转移的方式
#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
#define il inline
#define re register
const int N = 1e5 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int MOD = 1e9;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
int n, m, cnt, tot;
int dp[105][105][105];
int a[105][15], dat[1 << 15], state[100], num[100];
void init() {
tot = 0;
for (int i = 0; i < cnt; i++) {
if (!(i & (i >> 1)) && !(i & (i >> 2))) {
int tmp = i;
while (tmp) {
num[tot] += tmp % 2;
tmp >>= 1;
}
state[tot++] = i;
}
}
}
void solve() {
cin >> n >> m; cnt = 1 << m;
memset(dp, -1, sizeof dp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c; cin >> c;
if (c == 'P') a[i][j] = 1;
else a[i][j] = 0;
dat[i] = dat[i] << 1 | a[i][j];
}
}
init();
int ans = 0;
for (int i = 0; i < tot; i++) {
if ((state[i] & dat[1]) != state[i]) continue;
dp[1][i][0] = num[i];
ans = max(ans, dp[1][i][0]);
}
for (int i = 2; i <= n; i++) {
for (int j = 0; j < tot; j++) {
if ((state[j] & dat[i]) != state[j]) continue;
for (int k = 0; k < tot; k++) {
if ((state[k] & state[j])) continue;
for (int l = 0; l < tot; l++) {
if ((state[l] & state[k]) || (state[l] & state[j])) continue;
if (dp[i-1][k][l] == -1) continue;
dp[i][j][k] = max(dp[i][j][k], dp[i-1][k][l] + num[j]);
ans = max(ans, dp[i][j][k]);
}
}
}
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
solve();
}
Problem-Arrangement
#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
#define il inline
#define re register
const int N = 1e5 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int MOD = 1e9;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
ll a[15][15], dp[1 << 13][505];
ll f[15], cnt;
void solve() {
f[0] = 1;
for (int i = 1; i <= 13; i++) f[i] = f[i-1] * i;
int T; cin >> T; while (T--) {
memset(dp, 0, sizeof dp);
int n, m; cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cnt = 1 << n; dp[0][0] = 1;
for (int i = 0; i < cnt; i++) {
int num = 0;
for (int j = 0; j < n; j++) {
if (i >> j & 1) num++;
}
for (int j = 0; j < n; j++) {
if (!(i >> j & 1)) {
for (int k = 0; k <= m; k++) {
if (k + a[num][j] >= m) dp[i + (1 << j)][m] += dp[i][k];
else dp[i + (1 << j)][k + a[num][j]] += dp[i][k];
}
}
}
}
if (!dp[(1 << n) - 1][m]) cout << "No solution" << endl;
else cout << f[n] / __gcd(dp[(1 << n) - 1][m], f[n])<< "/" << dp[(1 << n) - 1][m] / __gcd(dp[(1 << n) - 1][m], f[n]) << endl;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
solve();
}
方格取数(1)
#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
#define il inline
#define re register
const int N = 1e5 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int MOD = 1e9;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ull;
ll dp[20000][25], a[25][25], cnt, state[20000], tot, sum[20000][25];
void init(int n) {
tot = 0;
for (int i = 0; i < 1 << n; i++) {
if (i >> 1 & i) continue;
state[tot++] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < tot; j++) {
for (int k = 1; k <= n; k++) {
if (state[j] >> (k - 1) & 1) sum[j][i] += a[i][k];
}
}
}
}
void solve() {
int n; while (cin >> n) {
memset(dp, 0, sizeof dp);
memset(sum, 0, sizeof sum);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
init(n);
for (int i = 0; i < tot; i++) dp[i][1] = sum[i][1];
for (int i = 2; i <= n; i++) {
for (int j = 0; j < tot; j++) {
for (int k = 0; k < tot; k++) {
if (state[j] & state[k]) continue;
dp[j][i] = max(dp[j][i], dp[k][i-1] + sum[j][i]);
}
}
}
ll ans = 0;
for (int i = 0; i < tot; i++) ans = max(ans, dp[i][n]);
cout << ans << endl;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
#endif
solve();
}