P2915 [USACO08NOV]Mixed Up Cows G
下一个选择的数之和当前的数有关,状压DP即可。
查看代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } typedef long long LL; const int N = 16, M = 1 << N; LL f[N][M]; int n, m, w[N]; int main () { read(n), read(m); for (int i = 0; i < n; i++) read(w[i]), w[i]--; for (int i = 0; i < n; i++) f[i][1 << i] = 1; for (int i = 0; i < 1 << n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) if (abs(w[j] - w[k]) > m && !(i >> k & 1)) f[k][i | 1 << k] += f[j][i]; LL res = 0; for (int i = 0; i < n; i++) res += f[i][(1 << n) - 1]; write(res); return 0; }
|