﻿ _bzoj1087 [SCOI2005]互不侵犯King【dp】

### _bzoj1087 [SCOI2005]互不侵犯King【dp】

f(i, j, k) = sigma(i - 1, p, k - num[j])，其中可以从p状态转化到j状态，num[j]表示j状态下的国王数。

```#include <cstdio>
#include <cstring>

int n, kk, num[515], S[515];
long long f[11][515][85], ans;
int head[515], next[512 * 512], to[512 * 512], lb;

inline void ist(int aa, int ss) {
to[lb] = ss;
++lb;
}

int main(void) {
memset(next, -1, sizeof next);
scanf("%d%d", &n, &kk);
for (int i = 0; i < (1 << n); ++i) {
S[i] = (1 << n) - 1;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
++num[i];
if (j) {
if (i >> (j - 1) & 1) {
num[i] = -1;
break;
}
S[i] &= (~(1 << (j - 1)));
}
S[i] &= (~(1 << j));
if (j < n - 1) {
S[i] &= (~(1 << (j + 1)));
}
}
}
}
for (int i = 0; i < (1 << n); ++i) {
if (num[i] == -1) {
continue;
}
for (int j = 0; j < (1 << n); ++j) {
if (num[j] != -1 && (S[i] | j) == S[i]) {
ist(i, j);
}
}
}

f[0][0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < (1 << n); ++j) {
for (int k = num[j]; k <= kk; ++k) {
for (int p = head[j]; p != -1; p = next[p]) {
f[i][j][k] += f[i - 1][to[p]][k - num[j]];
}
}
}
}
for (int j = 0; j < (1 << n); ++j) {
ans += f[n][j][kk];
}
printf("%lld
", ans);
return 0;
}
```