BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 状态压缩dp
开始读错题了,然后发现一眼切~
Code:
#include <cstdio> #include <algorithm> #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int s[20]; ll dp[1<<18][17],fac[20]; int main() { // setIO("input"); int n,k,i,j,l; scanf("%d%d",&n,&k); fac[0]=1; for(i=1;i<=n;++i) fac[i]=fac[i-1]*i; for(i=0;i<n;++i) scanf("%d",&s[i]), dp[(1<<i)][i]=1; for(l=1;l<(1<<n);++l) { for(j=0;(1<<j)<=l;++j) { if((l&(1<<j)) && dp[l][j]) { for(int p=0;(1<<p)<(1<<n);++p) { if(((1<<p)^l)&&abs(s[p]-s[j])>k){ dp[l^(1<<p)][p]+=dp[l][j]; } } } } } ll re=0; for(i=0;i<n;++i) re+=dp[(1<<n)-1][i]; printf("%lld\n",re); return 0; }