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

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.

 

题意:题目大意: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)的更多相关文章

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

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

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

    虽然是一道还是算简单的DP,甚至不用滚动数组也能AC,数据量不算很大. 对于N个数,每个数只存在两个状态,取 和 不取. 容易得出状态转移方程: dp[i][j] = dp[i - 1][j ^ a[ ...

  3. HDU 5119 Happy Matt Friends(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5119 题意:给出n个数和一个上限m,求从这n个数里取任意个数做异或运算,最后的结果不小于m有多少种取法. 思路: ...

  4. HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...

  5. HDU 5119 Happy Matt Friends

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...

  6. 水题: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 ...

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

    题意:给定n个数,从中分别取出0个,1个,2个...n个,并把他们异或起来,求大于m个总的取法. 思路:dp,背包思想,考虑第i个数,取或者不取,dp[i][j]表示在第i个数时,异或值为j的所有取法 ...

  8. HDU - 5119 Happy Matt Friends(dp)

    题目链接 题意:n个数,你可以从中选一些数,也可以不选,选出来的元素的异或和大于m时,则称满足情况.问满足情况的方案数为多少. 分析:本来以为是用什么特殊的数据结构来操作,没想到是dp,还好队友很强. ...

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

    题目链接 题意 : 给你n个数,让你从中挑K个数(K<=n)使得这k个数异或的和小于m,问你有多少种异或方式满足这个条件. 思路 : 正解据说是高斯消元.这里用DP做的,类似于背包,枚举的是异或 ...

随机推荐

  1. SpringBoot | 第十七章:web应用开发之文件上传

    前言 上一章节,我们讲解了利用模版引擎实现前端页面渲染,从而实现动态网页的功能,同时也提出了兼容jsp项目的解决方案.既然开始讲解web开发了,我们就接着继续往web这个方向继续吧.通常,我们在做we ...

  2. DB2去重的几种方法

    有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略. 例如下表:table1 用户办理套餐的记 ...

  3. <linux下extmail服务的搭建>

    下载2个软件包: extmail-1.1.0.tar.gz     extman-1.1.tar.gz 下载地址:http://www.cpan.org/ 创建extsuite目录 mkdir /va ...

  4. Ajax跨域设置Access-Control-Allow-Origin

    传统的跨域请求没有好的解决方案,无非就是jsonp和iframe,随着跨域请求的应用越来越多,W3C提供了跨域请求的标准方案(Cross-Origin Resource Sharing).IE8.Fi ...

  5. iOS开发ReactiveCocoa学习笔记(五)

    ReactiveCocoa常见操作方法介绍: demo地址:https://github.com/SummerHH/ReactiveCocoa.git filter ignore ignoreValu ...

  6. java-类的定义和用法

    1.类的定义 public class Human{ }//每个源文件必须也只能有一个public类 class boy{ }//可以定义多个class类 class girl{ } 上面的类定义好后 ...

  7. 面试(Java之IO与NIO)

    一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...

  8. RabbitMQ:消息发送确认 与 消息接收确认(ACK)

    默认情况下如果一个 Message 被消费者所正确接收则会被从 Queue 中移除 如果一个 Queue 没被任何消费者订阅,那么这个 Queue 中的消息会被 Cache(缓存),当有消费者订阅时则 ...

  9. linux下安装redis和部署

    转自简书:https://www.jianshu.com/p/bc84b2b71c1c 1.基础知识  redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值 ...

  10. nc扫描端口

    nc -n -v -z -w 1 ip地址 1-1000 (端口号) 详细信息 -v 排除dns  -n 不发送任何数据-z 超时设置为1秒 -w 1