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. 学习PS必须弄懂的专业术语

    在学习PS的过程中,我们经常会遇到一些专业术语,下面我们来对一些常用的.比较难理解的术语进行简单讲解. 像素:像素是构成图像的最基本元素,它实际上是一个个独立的小方格,每个像素都能记录它所在的位置和颜 ...

  2. web server服务器

    使用最多的 web server服务器软件有两个:微软的信息服务器(iis),和Apache. 通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序 ...

  3. eclipse注解快捷键

    Search 功能:全局文件内容搜索快捷键: Ctrl + H -------------------------------------------------------------------- ...

  4. 转:Grunt:任务自动管理工具

    Grunt:任务自动管理工具 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 安装 命令脚本文件Gruntfile.js Gruntfile.js实例:gru ...

  5. 原理图产生网络表后导进PADS之后,网络乱了的问题

    问题描述:在Orcad中生成的网络表(格式.ASC),导进PADS9.2中(PADS9.2中已有一些元器件),结果报Mixing nets,如下图示. 仔细检查原理图中的这些nets,发现有的有错,有 ...

  6. CCNA实验(3) -- RIP

    RIP协议分为版本1和版本2,均具备以下特征:1.是距离向量路由协议2.使用跳数(Hop Count)作为度量值3.默认路由更新周期为30秒4.管理距离(AD)为1205.支持触发更新6.最大跳数为1 ...

  7. SPOJ 7258 Lexicographical Substring Search(后缀自动机)

    [题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...

  8. 联想S720/S720i通刷刷机包 Vibe V1.0

    ROM介绍 基于官方最新S116底包制作,保证足够的稳定性. 增加VIBE元素,看起来更加大气.美观. 首次增加VIBE元素,720i执行起来无压力,720可能会有点卡.自行酌情刷入. 有bug请文明 ...

  9. SQL Server索引进阶:第一级,索引简介

    这个并不是我翻译的,全文共有15篇,但我发现好多网站已经不全,所以自己整理. 原文地址: Stairway to SQL Server Indexes: Level 1, Introduction t ...

  10. iOS 饼状图

    首先先看一下效果: 一.创建饼状图对象   创建饼状图对象用到类是PieChartView.h, 代码如下: self.pieChartView = [[PieChartView alloc] ini ...