Codeforces 327E Axis Walking 状压dp

这题真的有2500分吗。。。 难以置信。。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long

using namespace std;

const int N = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

int n, k, a[24], b[24], dp[1 << 24];
bool ban[1 << 24];
LL dis[1 << 24];

inline void add(int& a, int b) {
    a += b; if(a >= mod) a -= mod;
}

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    scanf("%d", &k);
    for(int i = 0; i < k; i++) scanf("%d", &b[i]);
    for(int S = 1; S < (1 << n); S++) {
        for(int i = 0; i < S; i++) {
            if(S >> i & 1) {
                dis[S] = dis[S ^ (1 << i)] + a[i];
                break;
            }
        }
        for(int i = 0; i < k; i++) {
            if(dis[S] == b[i]) {
                ban[S] = true;
                break;
            }
        }
    }
    dp[0] = 1;
    for(int S = 1; S < (1 << n); S++) {
        if(ban[S]) continue;
        for(int i = 0; i < n; i++) {
            if(S >> i & 1) {
                add(dp[S], dp[S ^ (1 << i)]);
            }
        }
    }
    printf("%lld
", dp[(1 << n) - 1]);
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/10475812.html