跳到正文

算法·理论

状压dp

Luna_From_Starry约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 13 分钟

版本

  1. v1当前

状压dp,全称(状态压缩dp)

  • 引入

  • 状压dp定义

    • 不用说了将dp状态进行压缩的dp,可以让我们更方便的推出状转
  • 状压dp分类

    • 1.环形

      • 题型及思路

        有时候,环上的不同的答案也是由区间划分的,此时我们就需要引入环形DP。

        首先,我们要明确环形DP的区间是怎样划分的,例如:

        给定一个环AA,即A1−A2,A2−A3,...,An−1−An,An−A1A_1-A_2,A_2-A_3,...,A_{n-1}-A_n,A_n-A_1(x-y 表示x与y有连边)。

        此时,我们从不同边切割,可以得到不同的链,如A1−A2−A3,An−1−An−A1A_1-A_2-A_3,A_{n-1}-A_n-A_1等。

        那么如何表示这些链呢?

        我们常用的做法是断环成链,将环在尾端扩展一倍,此时An−1−An−A1A_{n-1}-A_n-A_1可以表示为An−1−An−An+1A_{n-1}-A_n-A_{n+1},即区间[n−1,n+1][n-1,n+1]。

        那么学会了区间的表示,环形DP剩余的部分与区间DP是一样的。

      • 引入状压DP及具体实现思路

        二进制的拆分使得每一个数都可以表示为一些22的正整数幂次的和,而有些DP的状态转移依赖于集合的变化,所以此时利用一个二进制数表示集合:例如11=>(00001011)211=>(0000 1011)_2就可以表示集合内的元素有:1,2,41,2,4。注意元素的表达是从低位到高位表示的,即(01000)2(01000)_2表示的是44而不是22。
        • 别问我为什么没学就知道
    • 2.数组中做状压DP

      • 题型及思路

        假设棋盘要放置k个国王,国王对其周围八个格子都可发动进攻,如果要求这一行与上一行的国王不能相互进攻

        那么设上一行的状态为x(二进制数),这一行的状态为y的话,由于这一行放的国王的左上角,正上方,右上角都不能出现上一行的国王

        所以:

        正上方不出现上一行的国王的条件为:x&y==false

        左上角不出现上一行的国王的条件为:x&(y<<1)==false

        右上角不出现上一行的国王的条件为:x&(y>>1)==false

        通过简单的位运算判断,对于每一个y,我们都可以枚举所有x,并可以快速找出能够转移到y的合法的x的状态,从而使状态转移变得简单

        因此,对于这种题目,我们通常需要预处理出所有的x状态,以方便我们进行枚举

      • 通常做法:

        将每个状态(一个集合)用二进制表示,每个位的1就代表着这个编号的元素存在,0就代表着这个编号的元素不存在,如二进制100110,即是集合{1,2,5}的压缩

例题

  • [USACO06NOV] Corn Fields G

    我们观察这道题,发现其实草地的互相相邻只跟上一行(下一行更这一行有关)和自己这一行有关,那么我们就定义dp[i][j]为前i−1i - 1行已经确定且第ii行为j(2)j_{(2)}的方案数 而怎么判断是否合法呢 ? 我们枚举上一行为jj那么jj和ii只会出现四种情况
    • 1 和 1 出现了即为不合法
    • 1 和 0
    • 0 和 1
    • 0 和 0 我们可以通过按位与来判断上下两行合不合法
    • 1 & 1 = 1
    • 1 & 0 = 0
    • 0 & 1 = 0
    • 0 & 0 = 0 自己呢? 我们知道左右都为1则不合法,那么我们让座有两位对其是不是就行了呢 所以将j左移一位再和i按位与就行了
C++
#include<bits/stdc++.h>
#define int __int128
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
	cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
	string s=#__VA_ARGS__,s2="";\
	vector<string>v;\
	for(auto i:s){\
		if(i==','){\
			v.push_back(s2);\
			s2="";\
		}else{\
			s2+=i;\
		}\
	}\
	v.push_back(s2);\
	vector<int>v2={__VA_ARGS__};\
	for(int i=0;i<v.size()-1;i++){\
		cout<<v[i]<<"="<<v2[i]<<"\n";\
	}\
	cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 20;
AK M = (1 << 12) + 10;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline istream&operator>>(istream&is,int&k){
    int f=1;
    k=0;
    char ch=getchar();
    while(!isdigit(ch))(ch=='-'?f*=-1:f*=1),ch=getchar();
    while(isdigit(ch))k=(k<<3)+(k<<1),k+=ch-'0',ch=getchar();
    k*=f;
    return is;
}
inline ostream&operator<<(ostream&os,int k){
    if(k<0)putchar('-'),k*=-1;
    if(k>=10)os<<(k/10);
    putchar(k%10+'0');
    return os;
}
int dp[N][M], soil[N], mod = 1e8;
int n, m;
void work(){
    //please write your code here
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			int x;
			cin >> x;
			soil[i] = (soil[i] << 1) + x;
		}
	}
	dp[0][0] = 1;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < (1 << m); j++){
			if(((j & soil[i]) == j) && (((j << 1) & j) == 0)){
				for(int k = 0; k < (1 << m); k++){
					if((k & j) == 0){
						dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod;
					}
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < (1 << m); i++){
		ans = (ans + dp[n][i]) % mod;
	}
	cout << ans;
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t = 1; while(t--){work();}
	return 0;
}

由于判断范围增加了,枚举时多枚举一个就行了

C++
#include<bits/stdc++.h>
#define int __int128
#define Code using
#define by namespace
#define cmy std
#define AK const int
#define un unsigned
#define problem signed
#define T1 main
#define PII pair<int, int>
#define fi first
#define se second
#define un_m unorderd_map
#define un_s unorderd_set
#define debug(...){\
	cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
	string s=#__VA_ARGS__,s2="";\
	vector<string>v;\
	for(auto i:s){\
		if(i==','){\
			v.push_back(s2);\
			s2="";\
		}else{\
			s2+=i;\
		}\
	}\
	v.push_back(s2);\
	vector<int>v2={__VA_ARGS__};\
	for(int i=0;i<v.size()-1;i++){\
		cout<<v[i]<<"="<<v2[i]<<"\n";\
	}\
	cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
Code by cmy;
AK N = 20;
AK M = (1 << 12) + 10;
AK INF = 1e18;
int read() {int x = 0, w = 1;char ch = 0;while (!isdigit(ch)) {  if (ch == '-') w = -1;ch = getchar();}while (isdigit(ch)) { x = x * 10 + (ch - '0');  ch = getchar();}return x * w;}
void write(int x){if (x < 0) putchar('-'), x = -x;if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline istream&operator>>(istream&is,int&k){
    int f=1;
    k=0;
    char ch=getchar();
    while(!isdigit(ch))(ch=='-'?f*=-1:f*=1),ch=getchar();
    while(isdigit(ch))k=(k<<3)+(k<<1),k+=ch-'0',ch=getchar();
    k*=f;
    return is;
}
inline ostream&operator<<(ostream&os,int k){
    if(k<0)putchar('-'),k*=-1;
    if(k>=10)os<<(k/10);
    putchar(k%10+'0');
    return os;
}
int dp[101][(1 << 6)][(1 << 6)][21], cal[1 << 6], mod = 1e9 + 7;
int n, m;
void work(){
    //please write your code here
	cin >> n >> m;
	int p;
	cin >> p;
	for(int i = 0; i < (1 << n); i++){
		int j = i, cnt = 0;
		while(j){
			cnt += j % 2;
			j /= 2;
		}
		cal[i] = cnt;
	}
	dp[0][0][0][0] = 1;
	for(int i = 1; i <= m; i++){
		for(int j = 0; j < (1 << n); j++){
			for(int k = 0; k < (1 << n); k++){
				for(int l = 0; l < (1 << n); l++){
					for(int h = cal[j]; h <= p; h++){
						if(((j & (k << 1)) || (j & (k >> 1)) || (j & (l >> 2)) || (j & (l << 2))) == 0){
							dp[i][l][j][h] += (dp[i - 1][k][l][h - cal[j]]) % mod;
							dp[i][l][j][h] %= mod;
						}
					}
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < (1 << n); i++){
		for(int j = 0; j < (1 << n); j++){
			ans += dp[m][i][j][p];
			ans %= mod;
		}
	}
	cout << ans;
}
problem T1(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t = 1; while(t--){work();}
	return 0;
}
原文