状压dp

· · 算法·理论

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

例题

#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;
}
#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;
}