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的更多相关文章

  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(递推)

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

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

  4. HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)

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

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

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

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

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

  7. HDU - 5119 Happy Matt Friends(dp)

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

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

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

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

    题意:有最多40个数字,取任意个数字他们的异或和>=k则是可行的方案,问有多少种可行的方案. 分析:dp[now][j]表示当前这个值的种类数,那么转移方程为dp[now][j] = dp[pr ...

随机推荐

  1. Android开发--使用真机进行USB调试程序

    在android小程序的开发过程中,使用eclipse中的虚拟机进行程序开发速度较慢,用真机开发可以显著提高调试的速度. 这里我用的操作系统是win7专业版,手机型号HM1S: 进行USB调试的主要步 ...

  2. 架构探险——从零开始写Java Web框架》第二章照作

    沉下来慢慢看实现了. 越来越觉得可以和DJANGO作对比. package org.smart4j.chapter2.model; /** * Created by sahara on 2016/3/ ...

  3. struts2配置文件中action的name属性

    struts2配置文件中action的name属性的第一个字符不要加斜杠 <action name="see" class="baoxiuManage_seeAct ...

  4. hdu1233

    http://acm.hdu.edu.cn/showproblem.php?pid=1233 最小生成树,kruskal算法 #include<stdio.h> #include<m ...

  5. Linux shell 脚本小记

    if结构 #!/bin/env bash -gt ] then echo "$1 is positive" -lt ] then echo "$1 is negative ...

  6. Qt4升级Qt5注意问题

    Qt4升级Qt5注意问题 Qt4过渡到Qt5的项目一开始就受阻,记录一下遇到的下面的问题 --->编译遇到类似错误: error: QCalendarWidget: No such file o ...

  7. C语言复习笔记-17种小算法-解决实际问题

    判断日期为一年中的第几天(考虑闰年) 1 /* 2 * 计算该日在本年中是第几天,注意闰年问题 3 * 以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 4 * 特殊情况,闰年且 ...

  8. 249. Group Shifted Strings

    题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...

  9. iosUITextField属性

    @property UITextField *caption; caption = [[UITextField alloc] initWithFrame:CGRectMake(, self.frame ...

  10. Cookie的具体使用之来存储对象

    1.创建一个新的cookie,并赋值. HttpCookie cookie;       cookie=new HttpCookie("user");       cookie.D ...