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代码:
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cstring>
  5. using namespace std;
  6. const int INF=0x3f3f3f3f;
  7. const int MAXN=<<;
  8. int dp[][MAXN<<];//刚开始开到MAXN+10 RE,改成这才对;
  9. int main(){
  10. int T,N,M,flot=;
  11. int m[];
  12. scanf("%d",&T);
  13. while(T--){
  14. scanf("%d%d",&N,&M);
  15. memset(dp,,sizeof(dp));
  16. dp[][]=;
  17. int x=;
  18. for(int i=;i<N;i++){
  19. x^=;
  20. scanf("%d",m+i);
  21. for(int j=;j<=MAXN;j++){
  22. dp[x][j]=dp[x^][j]+dp[x^][j^m[i]];
  23. //代表上一次值为j^m[i]现在再^m[i]等于j了再加上上次j的个数;
  24. }
  25. }
  26. long long ans=;
  27. for(int i=M;i<=MAXN;i++)ans+=dp[x][i];
  28. printf("Case #%d: %lld\n",++flot,ans);
  29. }
  30. return ;
  31. }

暴力超时:

  1. #include<stdio.h>
  2. int ans,dt[];
  3. int N,M;
  4. void dfs(int top,int sox,int num,int t){
  5. if(num==t){
  6. if(sox>=M)ans++;
  7. return;
  8. }
  9. for(int i=top;i<N;i++){
  10. dfs(i+,sox^dt[i],num+,t);
  11. }
  12. }
  13. int main(){
  14. int T;
  15. scanf("%d",&T);
  16. while(T--){
  17. scanf("%d%d",&N,&M);
  18. for(int i=;i<N;i++)scanf("%d",dt+i);
  19. ans=;
  20. for(int i=;i<=N;i++){
  21. dfs(,,,i);
  22. }
  23. printf("%d\n",ans);
  24. }
  25. return ;
  26. }

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. OO真经——关于面向对象的哲学体系及科学体系的探讨(下)

    真经第六章——运作 Moving “运动是绝对的——牛顿” 6.1.导言 在前五章中,我们从世界观的这话题开始,逐步引出了抽象.层次.继承和耦合.这些内容,形成了对象论中关于世界的结构体系.      ...

  2. 记录hyperic-hq搭建开发环境遇到的坑

    这个星期接到一个新的任务:解决HQ(一个用JAVA开发的开源的运维监控平台)现在遇到的snmp升级到3.0后bug.公司用的HQ是4.6版本.于是,我把项目从gitlab上clone下来后,就开始了我 ...

  3. Spring总结——控制反转,注入(配置和注解两种方式)

    一.Spring的容器: 1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的(也可以说类A依赖类B),而在Spring中,则是由s ...

  4. T-SQL应用,视图、存储过程、触发器、游标、临时表等

    sqlserver常用操作: 视图.存储过程.触发器.函数 --*********************批处理********************* --[在一个批处理中存有一个语法错误,则所有 ...

  5. 0617 python 基础04

    控制流--for 循环 >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 换行输出 >>> for i in range(1 ...

  6. [LeetCode]题解(python):141-Linked List Cycle

    题目来源: https://leetcode.com/problems/linked-list-cycle/ 题意分析: 给定一个链表,判断链表是否有环.要求O(1)空间时间复杂度. 题目思路: 用快 ...

  7. IOS 特定于设备的开发:处理基本方向

    UIDevice类使用内置的orientation属性获取设备的物理方向.IOS设备支持这个属性的7个可能的值. >UIDeviceOrientationUnknown:方向目前未知. > ...

  8. Javascript 设计模式笔记

    设计模式太多了 还有些模式概念非常接近(比如观察者 中介者 和 事件发布/订阅模式) 构造器模式 var newObject = {} var newObject = new XXX(); 模块模式 ...

  9. HttpWebRequest使用注意(发生阻塞的解决办法)

    原文 http://www.cnblogs.com/Fooo/archive/2008/10/31/1323400.html HttpWebRequest使用注意(发生阻塞的解决办法) , count ...

  10. stl 迭代子的失效

    迭代子是STL中很重要的特性,但是其很脆弱(我个人认为),因为使用它的条件很苛刻,一不小心就失效了.其在两中情况下可能会失效. 1.当容器有插入操作时 在初始化了迭代子后,如果容器有插入操作时,迭代子 ...