1. 程式人生 > >HDU-5119 Happy Matt Friends (背包DP,遞推枚舉)

HDU-5119 Happy Matt Friends (背包DP,遞推枚舉)

思路 ring 滾動 style 背包dp 異或 space spa 多少

題意:n個物品,第i個物品的權值為ki,選出一些物品使它們的異或不小於m,求有多少種方案

數據範圍:1 <= n <= 40,0 <= m <= 1e6

思路:其實就是換了一種要求的背包,MX要開得比1e6大一些,不滾動數組也能過去。

老套路設dp[i][j]為前i個物品異或為j時的方案,dp[i][j] = dp[i-1][j] + dp[i-1][j^a[i]],最後把dp[n][m以上]的值求和

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4
#define LL long long 5 using namespace std; 6 7 const int mx = 1<<20; 8 int a[50]; 9 LL dp[50][mx+10]; 10 11 int main(){ 12 int t, kase = 0; 13 scanf("%d", &t); 14 while (t--){ 15 int n, m; 16 memset(dp, 0, sizeof dp); 17 LL ans = 0; 18 scanf("
%d%d", &n, &m); 19 for (int i = 1; i <= n; i++) 20 scanf("%lld", &a[i]); 21 dp[0][0] = 1; 22 for (int i = 1; i <= n; i++) 23 for (int j = 0; j <= mx; j++) 24 dp[i][j] = dp[i-1][j] + dp[i-1][j^a[i]]; 25 for
(int i = m; i < mx; i++) 26 ans += dp[n][i]; 27 printf("Case #%d: %lld\n", ++kase, ans); 28 } 29 return 0; 30 }

HDU-5119 Happy Matt Friends (背包DP,遞推枚舉)