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. hdu 3661 Assignments(水题的解法)

    题目 //最早看了有点云里雾里,看了解析才知道可以很简单的排序过 #include<stdio.h> #include<string.h> #include<algori ...

  2. POJ 1716

    #include <iostream> #include <algorithm> #define MAXN 20005 using namespace std; int _m[ ...

  3. POJ 1701

    /* Every tenant went up N floors would make the dissatisfied degree rise N * a + 0.5 * N * (N - 1) d ...

  4. C# 使用TimeSpan计算两个时间差

    转载:http://www.cnblogs.com/wifi/articles/2439916.html 可以加两个日期之间任何一个时间单位. private string DateDiff(Date ...

  5. SQL事物用法【转】

    SQL事务 一.事务概念 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作时事务是作为 ...

  6. Intent(二)

    以Android高级编程一书中的一个例子为例: 1, 创建一个ContactPicker项目,其中包含一个ContactPicker Activity package com.paad.contact ...

  7. [SQL Server 系] T-SQL数据库的创建与修改

    创建数据库 USE master; GO CREATE DATABASE ToyUniverse ON ( NAME = ToyUniverse_Data, FILENAME = 'F:\Projec ...

  8. Hibernate逍遥游记-第7章 Hibernate的检索策略和检索方式(<set lazy="false" fetch="join">、left join fetch、FetchMode.JOIN、)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  9. SVN与Eclipse整合

    SVN与Eclipse整合 下载SVN插件(http://subclipse.tigris.org) 我们使用版本eclipse_svn_site-1.6.5.zip 解压到一个文件夹中 进入ecli ...

  10. C++:调整基类成员在派生类中的访问属性的其他方法(同名成员和访问声明)

    4.3 调整基类成员在派生类中的访问属性的其他方法 4.3.1 同名函数 在定义派生类的时候,C++语言允许在派生类中说明的成员与基类中的成员名字相同,也就是 说,派生类可以重新说明与基类成员同名的成 ...