2014年北京 happy matt friends(dp + 滚动数组优化)
Happy Matt Friends
Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others)
Total Submission(s): 6164 Accepted Submission(s): 2330
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.
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.
/*
本题思路:用dp[i][j]表示前i个数可以凑出数字j的方法数目。
dp[i][j] = dp[i - 1][j ^ a[i]] + dp[i - 1][j];
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , maxm = << ;
int n, m; typedef long long ll; int a[maxn]; ll dp[][maxm]; int main() {
int t, _case = ;
scanf("%d", &t);
while(t --) {
memset(dp, , sizeof dp);
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i ++) {
scanf("%d", &a[i]);
}
/*
dp[0][0] = 1;
for(int i = 1; i <= n; i ++) {
for(int j = 0; j < maxm; j ++) {
dp[i][j] = dp[i - 1][j ^ a[i]] + dp[i - 1][j];
}
}
*/
dp[][] = ;
for(int i = ; i <= n; i ++) {
for(int j = ; j < maxm; j ++) {
dp[i & ][j] = dp[i - & ][j ^ a[i]] + dp[i - & ][j];
}
}
ll ans = ;
for(int i = m; i < maxm; i ++) {
ans += dp[n & ][i];
}
printf("Case #%d: %lld\n", ++ _case, ans);
}
return ;
}
2014年北京 happy matt friends(dp + 滚动数组优化)的更多相关文章
- HDU_1024.MaxSumPlusPlus(基础DP + 滚动数组优化讲解)
这道题打破了我常规的做题思路,因为这是我刚开始训练DP,感觉这道题目好晕眼呀,emm其实就是感觉自己是真的菜...... 为什么说打破了我的做题思路呢,因为我平时看题解都是在已经AC或者完全不懂的情况 ...
- [BZOJ1044][HAOI2008]木棍分割 二分 + 单调队列优化dp + 滚动数组优化dp
Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长 ...
- LG3004 「USACO2010DEC」Treasure Chest 区间DP+滚动数组优化
问题描述 LG3004 题解 把拿走的过程反向,看做添加的过程,于是很显然的区间DP模型. 设\(opt_{i,j}\)代表区间\([i,j]\)中Bessie可以获得的最大值,显然有 \[opt_{ ...
- poj1159 dp(滚动数组优化)
H - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:3000MS Memory Limit:65536KB ...
- 51Nod 1084 矩阵取数问题 V2 双线程DP 滚动数组优化
基准时间限制:2 秒 空间限制:131072 KB 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下,再从右下走到左上.第1遍时只能向下和向右走,第2遍时只能向 ...
- POJ 3666 Making the Grade (DP滚动数组)
题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- BZOJ-1925 地精部落 烧脑DP+滚动数组
1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
随机推荐
- [深度学习] pytorch学习笔记(1)(数据类型、基础使用、自动求导、矩阵操作、维度变换、广播、拼接拆分、基本运算、范数、argmax、矩阵比较、where、gather)
一.Pytorch安装 安装cuda和cudnn,例如cuda10,cudnn7.5 官网下载torch:https://pytorch.org/ 选择下载相应版本的torch 和torchvisio ...
- 什么是iframe及作用是什么?
一. iframe是什么及作用 iframe是嵌入式框架, 是html标签, 还是一个内联元素, iframe 元素会创建包含另外一个文档的内联框架(即行内框架) . 说白了, iframe用来在页面 ...
- Linux 下的tmpfs文件系统(/dev/shm)
介绍 /dev/shm/是一个使用就是tmpfs文件系统的设备,其实就是一个特殊的文件系统.redhat中默认大小为物理内存的一半,使用时不用mkfs格式化. tmpfs是Linux/Unix系统上的 ...
- 51 Nod 1069 Nim游戏
分析: a1 xor a2 xor a3 ... xor an !=0 则为必胜态 a1 xor a2 xor a3 ... xor an ==0 则为必败态 也就是说只要计算异或值,如果非零则A赢, ...
- TTTTTTTTTTTTTTTTTT POJ 2724 奶酪消毒机 二分匹配 建图 比较难想
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5004 Accepted: 1444 ...
- week5 作业
week5 作业 1.描述GPT是什么,应该怎么使用? 描述GPT之前要简单了解MBR分区,MBR(Main Boot Record)叫做主引导记录,其位于磁盘的最前端,由一段代码组成,共占用512个 ...
- 第五周学习总结&实验报告三
第五周课程总结 1.this和super的区别: this:访问本类中的属性,如果本类没有此属性则从父类中继续查找:访问本类中的方法,如果本类中没有此方法则从父类中继续查找:调用本类构造,必须放在构造 ...
- MySQL定时任务实现方法
类型一:每隔一分钟插入一条数据: 参数说明: DEFINER:创建者: ON COMPLETION [NOT] PRESERVE :表示当事件不会再发生的情况下,删除事件(注意特定时间执行的事件, ...
- 10、kubernetes之RBAC认证
一.kubectl proxy # kubectl proxy --port=8080 # curl http://localhost:8080/api/v1/ # curl http://local ...
- 在win7下,将QT集成到vs2010上
在网上查了很多,自己先是下载了一个5.2.0版本的,但在我的电脑上运行时老报错,一怒之下决定不再使用5.2.0版本的QT,而先择了更低版本的4.8.5版本,然后.....然后就成功了.谢天谢地,在这我 ...