HDU 5119 Happy Matt Friends
Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 3215 Accepted Submission(s): 1261
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’magic numbers is no less than M , Matt wins.
Matt wants to know the number of ways to win.
Input
The first line contains only one integer T , which indicates the number of test cases.
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.
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y indicates the number of ways where Matt can win.
Sample Input
2
3 2
1 2 3
3 3
1 2 3
Sample Output
Case #1: 4
Case #2: 2
Hint
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.
Source
2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)
解析:动态规划。对每个状态,可以扩展出2种状态:取a[i]和不取a[i]。用dp[i][j]表示前i个数里面异或值为j的方法数,则
dp[i][j] += dp[i-1][j];
dp[i][j^a[i]] += dp[i-1][j];
```
#include
#include
const int MAXN = 1e6+5;
int dp[45][2*MAXN]; //dp[i][j]表示前i个数里面异或值为j的方法数
int a[45], n, m;
void solve()
{
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= 1e6; ++j){
dp[i][j] += dp[i-1][j];
dp[i][j^a[i]] += dp[i-1][j];
}
}
long long res = 0;
for(int i = m; i <= 1e6; ++i)
res += dp[n][i];
printf("%I64d\n", res);
}
int main()
{
int t, cn = 0;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
printf("Case #%d: ", ++cn);
solve();
}
return 0;
}
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 ...
- HDU 5119 Happy Matt Friends(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5119 题意:给出n个数和一个上限m,求从这n个数里取任意个数做异或运算,最后的结果不小于m有多少种取法. 思路: ...
- 水题: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 (14北京区域赛 类背包dp)
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Oth ...
- HDU 5119 Happy Matt Friends(dp+位运算)
题意:给定n个数,从中分别取出0个,1个,2个...n个,并把他们异或起来,求大于m个总的取法. 思路:dp,背包思想,考虑第i个数,取或者不取,dp[i][j]表示在第i个数时,异或值为j的所有取法 ...
- HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)
虽然是一道还是算简单的DP,甚至不用滚动数组也能AC,数据量不算很大. 对于N个数,每个数只存在两个状态,取 和 不取. 容易得出状态转移方程: dp[i][j] = dp[i - 1][j ^ a[ ...
- HDU - 5119 Happy Matt Friends(dp)
题目链接 题意:n个数,你可以从中选一些数,也可以不选,选出来的元素的异或和大于m时,则称满足情况.问满足情况的方案数为多少. 分析:本来以为是用什么特殊的数据结构来操作,没想到是dp,还好队友很强. ...
- HDU 5119 Happy Matt Friends(DP || 高斯消元)
题目链接 题意 : 给你n个数,让你从中挑K个数(K<=n)使得这k个数异或的和小于m,问你有多少种异或方式满足这个条件. 思路 : 正解据说是高斯消元.这里用DP做的,类似于背包,枚举的是异或 ...
- HDU 5119 Happy Matt Friends ——(背包DP)
题意:有最多40个数字,取任意个数字他们的异或和>=k则是可行的方案,问有多少种可行的方案. 分析:dp[now][j]表示当前这个值的种类数,那么转移方程为dp[now][j] = dp[pr ...
随机推荐
- **tomcat简介之web.xml详解
一.Tomcat背景 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Group在完成GNUJSP1.0的开发以后,开始考虑在SUN的JSWDK基础上开发一个可以直接提供Web服务的JS ...
- 小米2S 连接Ubuntu Android Studio
1. 首先打开手机上的开发者选项,USB调试.拨号:*#*#717717#*#* ,手机会以Toast形式出现“……enable”字样.再次拨号可disable. 2. Ubuntu安装mtpfs: ...
- 日志logger
1.使用指定类初始化日志对象 在日志输出的时候,可以打印出日志信息所在类如:Logger logger = LoggerFactory.getLogger(com.Book.class); ...
- XE2编译出来的DLL的DLLMain的退出地方用到了halt0
DelphiXE2内存加模块升级版.支持32位和64位模块. 已转至新的博客 http://www.raysoftware.cn/?p=51 很多年以前写过内存加载DLL的一片技术. http://b ...
- java:抽象类和抽象函数
面向对象:先抽象后具体 抽象类也叫基类 抽象函数:只有函数的定义,没有函数体的函数, 语法:类必须定义为抽象类,才能调用抽象函数,抽象类里面可以没有抽象函数 abstract class Printe ...
- linux 查看某一端口的占用情况
查看某一端口的占用情况: lsof -i:端口号,例如查看端口21是否被占用 lsof -i: 实例:查看端口是否被占用,如果被占用结束掉该端口 [root@localhost splunk]# ls ...
- laravel速记(笔记)
命令行: php artisan controller:make UserController This will generate the controller at /app/controller ...
- 今天晚上 中国互联网被Struts2漏洞血洗
Apache官方今天晚上发布安全公告(官方编号S2-032/CVE编号CVE-2016-3081),Apache Struts2服务在开启动态方法调用(DMI)的情况下,可以被远程执行任意命令,安全威 ...
- ubuntu下android源码下载
步骤一: 首先保证你的ubuntu系统电脑可以顺利游览google,我们是将etc下 hosts替换掉,推荐hosts: http://laod.cn/hosts/2015-google...host ...
- 内核MKDEV(MAJOR, MINOR)宏
版本:linux-2.6.24.4宏: MKDEV(MAJOR, MINOR); 说明: 获取设备在设备表中的位置. MAJOR 主设备号 MINOR 次设 ...