HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)
Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 5188 Accepted Submission(s): 1985
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’magic numbers is no less than M , Matt wins.
Matt wants to know the number of ways to win.
For each test case, the first line contains two integers N, M (1 ≤ N ≤ 40, 0 ≤ M ≤ 106).
In the second line, there are N integers ki (0 ≤ ki ≤ 106), indicating the i-th friend’s magic number.
Case #2: 2
In the first sample, Matt can win by selecting:
friend with number 1 and friend with number 2. The xor sum is 3.
friend with number 1 and friend with number 3. The xor sum is 2.
friend with number 2. The xor sum is 2.
friend with number 3. The xor sum is 3. Hence, the answer is 4.
题意:题目大意:n个数,从中挑k个,使得这k个数的异或和不小于m,问有多少种挑选方法(0<=k<=n)
思路:dp[i][j]表示前 i 个数中选择一些使得异或和为j的方法数,转移方程:dp[i][j] = dp[i - 1][j] + dp[i - 1][j ^ a[i]],即等于前i-1个异或和为j的方法数(第i个数不需要进行异或)加上前i-1个异或和为j ^ a[i]的方法数(第i个数需要异或),因为j ^ a[i] ^ a[i] == j ^ (a[i] ^ a[i]) == j ^ 0 = j,最后再累计一下j大于等于m时的方法数,这题内存给的够大,也可以直接采用滚动数组
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define ll long long
int const maxn = (<<);
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int a[];
ll dp[][maxn];
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--)
{
int n,m; memset(dp,,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
dp[][]=;
for(int i=;i<=n;i++)
for(int j=;j<maxn;j++)
dp[i][j]=dp[i-][j]+dp[i-][j^a[i]]; //如果前i-1个数异或值是j就不需要将第i个数进行异或,如果前i-1个数有异或后是j^a[i]的,那么第i个数进行异或,刚好可以得j
ll ans=;
printf("Case #%d: ",ca++);
for(int i=m;i<maxn;i++)
ans+=dp[n][i];
printf("%lld\n",ans);
}
return ;
}
HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)的更多相关文章
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)
虽然是一道还是算简单的DP,甚至不用滚动数组也能AC,数据量不算很大. 对于N个数,每个数只存在两个状态,取 和 不取. 容易得出状态转移方程: dp[i][j] = dp[i - 1][j ^ a[ ...
- HDU 5119 Happy Matt Friends(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5119 题意:给出n个数和一个上限m,求从这n个数里取任意个数做异或运算,最后的结果不小于m有多少种取法. 思路: ...
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...
- HDU 5119 Happy Matt Friends
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...
- 水题:HDU 5119 Happy Matt Friends
Matt has N friends. They are playing a game together.Each of Matt's friends has a magic number. In t ...
- HDU 5119 Happy Matt Friends(dp+位运算)
题意:给定n个数,从中分别取出0个,1个,2个...n个,并把他们异或起来,求大于m个总的取法. 思路:dp,背包思想,考虑第i个数,取或者不取,dp[i][j]表示在第i个数时,异或值为j的所有取法 ...
- HDU - 5119 Happy Matt Friends(dp)
题目链接 题意:n个数,你可以从中选一些数,也可以不选,选出来的元素的异或和大于m时,则称满足情况.问满足情况的方案数为多少. 分析:本来以为是用什么特殊的数据结构来操作,没想到是dp,还好队友很强. ...
- HDU 5119 Happy Matt Friends(DP || 高斯消元)
题目链接 题意 : 给你n个数,让你从中挑K个数(K<=n)使得这k个数异或的和小于m,问你有多少种异或方式满足这个条件. 思路 : 正解据说是高斯消元.这里用DP做的,类似于背包,枚举的是异或 ...
随机推荐
- 内核的执行头程序head.S
功能 定义data段和text段 重新手动初始化gdt表, idt表, tss表结构 初始化页表和页目录 --> 页目录的数据放在一个页表中 在页目录中, 其实地址为0x1000, 初始化页目录 ...
- 《疯狂动物城》主题曲《TryEverything》中文翻译
<疯狂动物城>主题曲<TryEverything>夏奇拉激情献唱,很多事情是需要是试试,不试试就不知道可以成功. Oh oh oh oh oooh 哦哦哦哦哦 Oh oh oh ...
- iQuery stop()
jQuery stop() 方法 jQuery stop() 方法用于停止动画或效果,在它们完成之前. stop() 方法适用于所有 jQuery 效果函数,包括滑动.淡入淡出和自定义动画. 语法 $ ...
- Eucalyptus镜像管理
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- C#执行异步操作的几种方式比较和总结(转发:https://www.cnblogs.com/durow/p/4826653.html)
0x00 引言 之前写程序的时候在遇到一些比较花时间的操作例如HTTP请求时,总是会new一个Thread处理.对XxxxxAsync()之类的方法也没去了解过,倒也没遇到什么大问题.最近因为需求要求 ...
- C#实现正则表达式
如果想了解正则表达式的基础知识:http://www.cnblogs.com/alvin-niu/p/6430758.html 一.C#中的Regex类 1.在C#中开发正则表达式,首先要引用Syst ...
- SSH中懒加载异常--could not initialize proxy - no Session
SSH进行关联的表进行显示时出现的问题,老是显示你的OGNL表达式错误,但是找了很久确实没错,在网上找了一下,下面的这个方法本人认为是最有效的方法(已经测试可以使用) 在web.xml中加入 程序代码 ...
- coursera_ML_1
机器学习定义: A computer program is said to leran from experience E with respect to some task T and some ...
- linux 命令——41 ps(转)
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- httpd2.4.6三种工作模式(如何配置),防止占用内存暴增的策略
之前偷懒默认用yum安装了httpd.后来发现服务器内存暴增,一度达到75% 打开一看,好嘛后台休眠进程全是httpd. 重启之后再度访问发现内存还是稳步增长. [root@iz2ze3ayxs2yp ...