最近做了好多“奶牛题”
比较简单的状压DP
直接记录集合无法转移,还要再加上一维,表示末尾的牛的编号
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
LL read() {
LL k = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9')
k = k * 10 + c - 48, c = getchar();
return k * f;
}
LL a[20], f[1 << 16][20];
int main() {
int n = read(), k = read();
for(int i = 0; i < n; ++i) a[i] = read(), f[1 << i][i] = 1;
for(int s = 1; s <= (1 << n) - 1; ++s)
for(int i = 0; i < n; ++i)
if((1 << i) & s)
for(int j = 0; j < n; ++j)
if(i != j && ((1 << j) & s) && abs(a[i] - a[j]) > k) f[s][i] += f[s ^ (1 << i)][j];
LL ans = 0;
for(int i = 0; i < n; ++i)
ans += f[(1 << n) - 1][i];
cout << ans;
return 0;
}