bzoj1231 [Usaco2008 Nov]mixup2 混乱的奶牛

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1231

【题解】

状压dp!f[S][i]表示状态为S,最后一个奶牛为i的方案数,枚举前一个奶牛判断即可。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = (1 << 17) + 10, N = 23;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, K, a[N], t[N], tn; 
ll f[M][N];
inline int ABS(int x) {return x>=0 ? x : -x;} 

int main() {
    cin >> n >> K;
    for (int i=1; i<=n; ++i) cin >> a[i];
    for (int i=1; i<=n; ++i) f[1<<i-1][i] = 1; 
    for (int sta = 1; sta < (1<<n); ++sta) {
        tn = 0; 
        for (int i=1; i<=n; ++i) if(sta & (1<<i-1)) t[++tn] = i; 
        if(tn == 1) continue;
        for (int i=1; i<=tn; ++i)
            for (int j=1; j<=tn; ++j) {
                if(j == i) continue;
                if(ABS(a[t[j]] - a[t[i]]) > K) 
                    f[sta][t[i]] += f[sta^(1<<t[i]-1)][t[j]];
            }
    }
    ll ans = 0;
    for (int i=1; i<=n; ++i) ans += f[(1<<n)-1][i]; 
    cout << ans;
    return 0;
}
View Code

 

posted @ 2017-06-07 15:18  Galaxies  阅读(249)  评论(1编辑  收藏  举报