标签:就是 friend 递推 amp friends 滚动 return end 数组
题意: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,递推枚举)
标签:就是 friend 递推 amp friends 滚动 return end 数组
原文地址:https://www.cnblogs.com/QAQorz/p/9035509.html