Happy Matt Friends(dp)
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
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.
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.
case number (starting from 1) and y indicates the number of ways where
Matt can win.
3 2
1 2 3
3 3
1 2 3
Case #2: 2
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.
#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)的更多相关文章
- 2014年北京 happy matt friends(dp + 滚动数组优化)
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Oth ...
- HDU 5119 Happy Matt Friends(dp+位运算)
题意:给定n个数,从中分别取出0个,1个,2个...n个,并把他们异或起来,求大于m个总的取法. 思路:dp,背包思想,考虑第i个数,取或者不取,dp[i][j]表示在第i个数时,异或值为j的所有取法 ...
- HDU - 5119 Happy Matt Friends(dp)
题目链接 题意:n个数,你可以从中选一些数,也可以不选,选出来的元素的异或和大于m时,则称满足情况.问满足情况的方案数为多少. 分析:本来以为是用什么特殊的数据结构来操作,没想到是dp,还好队友很强. ...
- HDU 5119 Happy Matt Friends(DP || 高斯消元)
题目链接 题意 : 给你n个数,让你从中挑K个数(K<=n)使得这k个数异或的和小于m,问你有多少种异或方式满足这个条件. 思路 : 正解据说是高斯消元.这里用DP做的,类似于背包,枚举的是异或 ...
- hdoj 5119 Happy Matt Friends 背包DP
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...
- HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Oth ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- 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. ...
- HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)
虽然是一道还是算简单的DP,甚至不用滚动数组也能AC,数据量不算很大. 对于N个数,每个数只存在两个状态,取 和 不取. 容易得出状态转移方程: dp[i][j] = dp[i - 1][j ^ a[ ...
随机推荐
- Java学习之字符串的创建
转自:http://lavasoft.blog.51cto.com/62575/80034/ Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时 ...
- mvn command is not recognized as an internal or external command
even though I have configured %m2_home% and %path% correctly, the command "mvn" is still n ...
- 安装爬虫scrapy
使用easy_install安装scrapy,报错 error: Setup script exited with error: command 'gcc' failed with exit stat ...
- iReport 4.1 报表制作,子报表,实例解析
开发使用步骤(iReport 4.1.1) (个人总结,如有问题请留言,另外知道table控件用法的给我留言或者发邮件谢谢.Email:jiazx0107@163.com) 目录 1. 开发 ...
- 四轴飞行器1.2.3 STM32F407时钟配置和升级标准库文件
原创文章,欢迎转载,转载请注明出处 这个星期进度比较慢哈,只有周末和晚上下班回来才能做,事件不连续,琐碎的事情又比较多,挺烦的,有多琐碎呢? 1.本人有点小强迫症哈,虽然RTT将文 ...
- oracle 数据库数据迁移解决方案
大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁移,接下来把心得与大家分享一下 去年年底做了不少系统的数据迁移,大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁 ...
- .NET(C#):XmlArrayItem特性和XmlElement特性在序列化数组的差别
原文http://www.cnblogs.com/mgen/archive/2011/12/04/2276238.html 比如这样一个类,我们用XmlArrayItem特性标明数组内出现的元素类型: ...
- 转:CSS Overflow 属性
原文:CSS Overflow 属性译自:The CSS Overflow Property版权所有,转载请注明出处,多谢!! 根据CSS的盒模型概念,页面中的每个元素,都是一个矩形的盒子.这些盒子的 ...
- JqGrid使用经验
一.更改分页组件的样式 分页组件中
- C++中delete 和delete[]的区别
c++告诉我们在回收new分配的单个对象的内存空间的时候用delete, 回收new[ ]分配的一组对象的内存空间的时候用 delete[ ]; #include <iostream> ...