Happy Matt Friends

Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 1810    Accepted Submission(s): 715

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范围为40,权值范围为10^6 约等于2的20次方,可以考虑类似dp+位运算的做法。因为异或值一定不会大于2^20,
所以可以枚举这个值,所以可以列出方程f[i][j]=f[i][j]+f[i][j^a[i]];
另外我用暴力也写了一发,虽然知道10000%超时;
dp代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=<<;
int dp[][MAXN<<];//刚开始开到MAXN+10 RE,改成这才对;
int main(){
int T,N,M,flot=;
int m[];
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
memset(dp,,sizeof(dp));
dp[][]=;
int x=;
for(int i=;i<N;i++){
x^=;
scanf("%d",m+i);
for(int j=;j<=MAXN;j++){
dp[x][j]=dp[x^][j]+dp[x^][j^m[i]];
//代表上一次值为j^m[i]现在再^m[i]等于j了再加上上次j的个数;
}
}
long long ans=;
for(int i=M;i<=MAXN;i++)ans+=dp[x][i];
printf("Case #%d: %lld\n",++flot,ans);
}
return ;
}

暴力超时:

 #include<stdio.h>
int ans,dt[];
int N,M;
void dfs(int top,int sox,int num,int t){
if(num==t){
if(sox>=M)ans++;
return;
}
for(int i=top;i<N;i++){
dfs(i+,sox^dt[i],num+,t);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
for(int i=;i<N;i++)scanf("%d",dt+i);
ans=;
for(int i=;i<=N;i++){
dfs(,,,i);
}
printf("%d\n",ans);
}
return ;
}

Happy Matt Friends(dp)的更多相关文章

  1. 2014年北京 happy matt friends(dp + 滚动数组优化)

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

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

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

  3. HDU - 5119 Happy Matt Friends(dp)

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

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

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

  5. hdoj 5119 Happy Matt Friends 背包DP

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

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

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

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

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

  8. TZOJ 5962 Happy Matt Friends(计数DP)

    描述 Matt hzs N friends. They are playing a game together. Each of Matt’s friends has a magic number. ...

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

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

随机推荐

  1. BZOJ 1876: [SDOI2009]SuperGCD( 更相减损 + 高精度 )

    更相减损,要用高精度.... --------------------------------------------------------------- #include<cstdio> ...

  2. Hbase Region Server 启动失败

    错误如下:Master rejected startup because clock is out of sync org.apache.hadoop.hbase.ClockOutOfSyncExce ...

  3. [LeetCode]题解(python):136-Single Number

    题目来源: https://leetcode.com/problems/single-number/ 题意分析: 给定一个数组,每个数都出现了2次,只有一个出现了一次,找出这个数.要求时间复杂度O(n ...

  4. 激活工具 – Microsoft Toolkit 2.4.7

    Microsoft Toolkit是一款很出名的Windows/Office激活工具,最早是因为激活Office 2010出名的,想必不少人也用过吧?Microsoft Toolkit从2.4.1版本 ...

  5. HTTP使用BASIC认证的原理及实现方法(还有NTLM方法,比较复杂)

    一.   BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务 器进行数据请求时,如果客 ...

  6. 64位WINDOWS系统环境下应用软件开发的兼容性问题(CPU 注册表 目录)

    应用软件开发的64 位WINDOWS 系统环境兼容性 1. 64 位CPU 硬件 目前的64位CPU分为两类:x64和IA64.x64的全称是x86-64,从名字上也可以看出来它和 x86是兼容的,原 ...

  7. Uber在华从沸点到冰点 搞定这些才能继续走下去

    腾讯科技 蒋栩根 5月6日报道 五一前夕,本应坐等笑看小长假带来的喜人业绩的Uber被广州市工商.交委.公安部门的联合检查当头浇了一盆冷水,也被戴上了一顶“涉嫌非法运营”的帽子.而一天前,它才与广物汽 ...

  8. c 跟字符串有关的函数

    1.字符串查找 strstr char * strstr(const char *s1, const char *s2); 在s1中查找s2,如果找到返回首个s2的首地址 char * strcase ...

  9. SQL Server索引进阶:第七级,过滤的索引

    原文地址: Stairway to SQL Server Indexes: Level 7,Filtered Indexes 本文是SQL Server索引进阶系列(Stairway to SQL S ...

  10. js柯里化的一个应用

    听到同学说面试一道题目 add(1)(2)(3)(4); 查询了下资料  这是一个js里面的柯里化 现象 add_curry防范返回的是一个 retVal,并不是执行结果.这里的代码很想递归,但是不是 ...