bzoj 1231 [Usaco2008 Nov]mixup2 混亂的奶牛
阿新 • • 發佈:2018-06-01
tin code span mod else long long scan IV fine
思路:比較裸的狀壓 dp[ i ][ s ][ 0 ] 表示已經加入的牛的情況為s, 最後一個為i 的 混亂種數,
dp[ i ][ s ][ 1 ]表示不混亂種數。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define pii pair<int,int> #define piii pair<int, pair<int,int> > using namespacestd; const int N = 16; const int M = 10 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const int zero = 160; int n, k, a[N]; LL dp[N][1 << N][2]; int main() { scanf("%d%d", &n, &k); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); dp[i][1 << i][0] = 1; } int up = 1 << n; for(int s = 1; s < up; s++) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(s & (1 << j)) continue; if(abs(a[i] - a[j]) <= k) { dp[j][s| (1 << j)][1] += dp[i][s][0]; } else { dp[j][s | (1 << j)][0] += dp[i][s][0]; } dp[j][s | (1 << j)][1] += dp[j][s][1]; } } } LL ans = 0; for(int i = 0; i < n; i++) ans += dp[i][up - 1][0]; printf("%lld\n", ans); return 0; } /* */
bzoj 1231 [Usaco2008 Nov]mixup2 混亂的奶牛