Maximum XOR Sum 系列问题
给定 $n$ 个两两不同的正整数 $a_1, a_2, \dots, a_n$,$a_i < 2^k$ 。
Problem 1(经典问题)
求 $a_i \xor a_j$ 的最大值,$ 1\le i, j \le n $ 。
解法
字典树
Problem 2
从 $n$ 个数中任选出一些数,求异或和的最大值。
Let the length of a number be the number of digits needed to write it out in binary, excluding any leading zeros.
Clearly, if all the input numbers had a different length, the problem would have a trivial solution: just iterate over the input numbers in decreasing order by length, choosing each number if and only if XORing it with the maximum so far increases the maximum, i.e., if and only if its leading bit is not set in the current maximum.
The tricky part is when the input may contain multiple numbers with the same length, since then it's not obvious which of them we should choose to include in the XOR. What we'd like to do is reduce the input list into an equivalent form that doesn't contain more than one number of the same length.
Conveniently, this is exactly what Gaussian elimination does: it transforms a list of vectors into another list of vectors which have strictly decreasing length, as defined above (that is, into a list which is in echelon form), but which spans the same linear subspace.
The reason this linear algebra algorithm is relevant here is that binary numbers satisfy the axioms of a vector space over the finite field of two elements, a.k.a. GF(2), with the number viewed as vectors of bits, and with XOR as the vector addition operation. (We also need a scalar multiplication operation to satisfy the axioms, but that's trivial, since the only scalars in GF(2) are $1$ and $0$.)
The linear subspace spanned by a set of bit vectors (i.e. binary numbers) over GF(2) is then simply the set of vectors obtainable by XORing a subset of them. Thus, if we can use Gaussian elimination to convert our input list into another one, which spans the same subspace, we can solve the problem using this other list and know that it gives the same solution as for the original problem.
Thus, we need to implement Gaussian elimination over GF(2).
// a[i] < (1LL << 60)
long long max_xor_sum(vector<long long> a, int n) {
long long res = 0;
int index = 0;
for (int column = 59; column >= 0; --column) {
long long mask = 1LL << column;
for (int row = index; row < n; ++row) {
if (a[row] & mask) {
swap(a[row], a[index]);
for (int row_ = row + 1; row_ < n; ++row_) {
if (a[row_] & mask) {
a[row_] ^= a[index];
}
}
if ((res & mask) == 0) {
res ^= a[index];
}
++index;
break;
}
}
}
return res;
}
References
https://math.stackexchange.com/a/1054206/538611
Problem 3
AtCoder Beginner Contest 141 Task F Xor Sum 3
Problem Statment
We have $N$ non-negative integers: $A_1, A_2, \dots, A_n$.
Consider painting at least one and at most $N − 1$ integers among them in red, and painting the rest in blue.
Let the beauty of the painting be the XOR of the integers painted in red, plus the XOR of the integers painted in blue.
Find the maximum possible beauty of the painting.
Constraints
- All values in input are integers.
- $2 \le N \le 10^5$
- $0 \le A_i < 2^{60} \ (1 \leq i \leq N)$
解法
此问题可转化为 Problem 2。
若第 $i$ 个二进制位为 1 的数共有奇数个,则不论如何划分,两部分的异或和在第 $i$ 位上必然一个是 1,一个是 0。
我们只需要考虑共有偶数个 1 的那些二进制位,在这些位上,不论如何划分,两部分的异或和一定是相等的,因此我们的目标是使这些位上的异或和最大,于是问题转化为 Problem 2。
代码 https://atcoder.jp/contests/abc141/submissions/7551333
Maximum XOR Sum 系列问题的更多相关文章
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- LeetCode(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
- LeetCode124:Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- [lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
随机推荐
- Mockito 2 让我们校验一些行为
在下面的示例中,我们将会模拟(Mock)一个 List 列表. 这是因为绝大部分的人对列表这个接口比较熟悉(例如 add(), get(), clear() 方法). 在实际情况中,请不要 mock ...
- [sdoi 2010][bzoj 1925]地精部落(神仙dp)
Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi, ...
- shell 之 用linux定时任务crontab和watchdog.sh脚本做软件看门狗
1.简介 看门狗的作用是定期检测服务正常运行,如果发现服务不在了,会重新拉起服务:linux中可以利用系统的定时任务功能crontab定期的去执行watchdog.sh脚本,而watchdog.sh脚 ...
- Java EE 之 Hibernate异常解决:org.hibernate.exception.SQLGrammarException: could not execute statement
本质原因:配置的Java Bean,由Hibernate自动产生的SQL语句中有语法错误 原因如下: 情况1.存在字段名/表名与数据库关键字冲突 情况2.MySQL5.0以后与MySQL5.0以前事务 ...
- Nginx之监控进程和工作进程
1. 函数调用分析 在开启 master 的情况下,多进程模型的下的入口函数为 ngx_master_process_cycle,如下: int mian() { ... if (ngx_proces ...
- 尚硅谷Docker---1、docker杂记
尚硅谷Docker---1.docker杂记 一.总结 一句话总结: ~ php用的homestead就相当于docker,javaee一般都是用docker,php也可以用docker ~ dock ...
- lockfree buffer test
性能测试(3): 对无锁队列boost::lockfree::queue和moodycamel::ConcurrentQueue做一个性能对比测试 版权声明:本文为博主zieckey原创文章, ...
- 执行docker run命令时报错Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
一.解决办法: 修改host 二.步骤如下 2.1 安装dig工具 sudo apt-get install dnsutils -y (ubuntu下的安装方法) 2.2 找到registry-1. ...
- MySQL 插件之 连接控制插件(Connection-Control)
目录 插件介绍 插件安装 插件配置 插件介绍 MySQL 5.7.17 以后提供了Connection-Control插件用来控制客户端在登录操作连续失败一定次数后的响应的延迟.该插件可有效的防止客户 ...
- 链表反转 C++
ListNode* reverse1(ListNode* pHead) { if(pHead == NULL) return NULL; ListNode * p1 = NULL; ListNode ...