洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 状压动归
考场上空间开大了一倍就爆0了QAQ…
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int maxn = 21; int height[30], pos[30]; long long dp[1 << maxn][21]; int main() { int n, delta; scanf("%d%d",&n,&delta); for(int i = 1;i <= n + 1; ++i) pos[i] = (1 << (i - 1)) ; for(int i = 1;i <= n; ++i) scanf("%d",&height[i]); for(int i = 1;i <= n; ++i) dp[pos[i]][i] = 1; for(int i = 1;i < pos[n + 1]; ++i) { for(int j = 1;j <= n; ++j) { if((i & pos[j]) == 0) continue; int mx = i ^ pos[j]; if(mx == 0) continue; for(int k = 1;k <= n; ++k) { if( (i & pos[k]) == 0 || k == j || abs(height[j] - height[k]) <= delta) continue; dp[i][j] += dp[mx][k]; } } } long long fin = 0; for(int i = 1;i <= n; ++i) fin += dp[pos[n + 1] - 1][i]; printf("%lld",fin); return 0; }