hdu 5119 Happy Matt Friends

http://acm.hdu.edu.cn/showproblem.php?pid=5119

题意:有n个数,然后从中挑选任意多的数进行异或,问异或出的值大于等于M的方案数多少?

思路:转移方程f[i][j]=f[i-1][j]+f[i-1][j^a[i]].  可以枚举这个值,对每个状态,来源有两个——一是上一个阶段不取,二是取a[i].

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

时间: 2024-08-26 01:07:18

hdu 5119 Happy Matt Friends的相关文章

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma

HDU 5119 Happy Matt Friends(简单二维dp)

题意不再说了,就是一个二维的dp,维持取最大值是多少. Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others) Total Submission(s): 608    Accepted Submission(s): 229 Problem Description Matt has N friends. They are playing a ga

HDU 5119 Happy Matt Friends(DP)

求解方案数的简单DP,比赛时没有往DP上想,思维比较局限. 状态转移很好写,类似于背包,我用记忆化搜索写的容易写,但是效率比较低,还占内存,读者可以改成递推式,还可以改成滚动数组,因为每一层的状态只用到它上一层的状态 . 细节参见代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<map

HDU 5119 Happy Matt Friends(DP)

题目链接:传送门 题意: 给定n个数,求从中选出任意个数异或起来值大于m的方案数. 分析: 动态规划,设dp[i][j] 表示第几次选第i个数的时候异或起来 值为j的方案数.dp[i][j^a[i]]+=dp[i][j];但是对空间有要求 我们可以用滚动数组来写. 代码如下: #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn = 1&l

HDU 5119 Happy Matt Friends(dp+位运算)

题意:给定n个数,从中分别取出0个,1个,2个...n个,并把他们异或起来,求大于m个总的取法. 思路:dp,背包思想,考虑第i个数,取或者不取,dp[i][j]表示在第i个数时,异或值为j的所有取法.dp[i][j] = dp[i - 1][j] + dp[i - 1][j ^ a[i]]); 其中dp[i - 1][j]表示不取第i个数,dp[i - 1][j & a[i]]表示取第i个数,由于40比较大,所以用滚动数组优化,后一个状态需要前一个来推导,而和前一个之前的所有的没有关系,所以之

HDU 5119 Happy Matt Friends ——(背包DP)

题意:有最多40个数字,取任意个数字他们的异或和>=k则是可行的方案,问有多少种可行的方案. 分析:dp[now][j]表示当前这个值的种类数,那么转移方程为dp[now][j] = dp[pre][j] + dp[pre][j^a[i]].因为a^b=c的话,c^b=a,所以j^a[i]就可以得到通过异或a[i]转移到j的那个状态了.然后可以用滚动数组压缩一下. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #inc

HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)

虽然是一道还是算简单的DP,甚至不用滚动数组也能AC,数据量不算很大. 对于N个数,每个数只存在两个状态,取 和 不取. 容易得出状态转移方程: dp[i][j] = dp[i - 1][j ^ a[i]] + dp[i - 1][j]; dp[i][j] 的意思是,对于数列 中前 i 个数字,使得 XOR 和恰好为 j 的方案数 状态转移方程中的 dp[i - 1][j] 即表示当前这个数字不取, dp[i - 1][j ^ a[i]] 表示当前这个数字要取. 这道题还是要好好理解阿! sou

HDU 5119 Happy Matt Friends(DP || 高斯消元)

题目链接 题意 : 给你n个数,让你从中挑K个数(K<=n)使得这k个数异或的和小于m,问你有多少种异或方式满足这个条件. 思路 : 正解据说是高斯消元.这里用DP做的,类似于背包,枚举的是异或的和,给定的数你可以选择放或者不放,dp[i][j]代表的是前 i 个数中选择k个异或的和为j. 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #define LL long long

hdu 5119 dP

E - Happy Matt Friends Time Limit:6000MS     Memory Limit:510000KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5119 Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the g