给定 $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 系列问题的更多相关文章

  1. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  2. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

  3. [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. ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. [sdoi2015]排序(搜索+剪枝优化)

    Description 小A有一个1-2^N的排列A[1..2^N],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1<=i<=N),第i中 ...

  2. 面试题:MySQL索引为什么用B+树?

    面试题:MySQL索引为什么用B+树? 前言 讲到索引,第一反应肯定是能提高查询效率.例如书的目录,想要查找某一章节,会先从目录中定位.如果没有目录,那么就需要将所有内容都看一遍才能找到. 索引的设计 ...

  3. CF1216E Numerical Sequence

    题目链接 问题分析 奇奇怪怪的题... 首先思路达成一致,从大到小一步一步确定位置. 我们一边分析,一边讲算法. 1121231234123451234561234567123456781234567 ...

  4. compare across commits online

    https://gist.github.com/nevik/5689882 Examples: https://github.com/octocat/Spoon-Knife/compare/ed122 ...

  5. Python做域用户验证登录

    安装包 ldap3 代码: from ldap3 import Server, Connection, ALL, NTLM # 连接 server = Server('public.ad.com', ...

  6. T89379 【qbxt】复读警告

    T89379 [qbxt]复读警告 题解 这是一道DP题 设置状态  f[ i ][ j ]  前 i 个数中所选数字之和 % key 得 j 的最大方案数 当前我们该选择第 i 个数字了,那么这个数 ...

  7. C#汉字转换拼音技术详解(高性能)

    public static class ChineseToPinYin { private static readonly Dictionary<<span class="key ...

  8. Spring Bean学习创建及使用<二>

    转自:http://blessht.iteye.com/blog/1162131 平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的 ...

  9. Zabbix - 实现对磁盘动态监控

        回到目录 前言 zabbix一直是小规模互联网公司服务器性能监控首选,首先是免费,其次,有专门的公司和社区开发维护,使其稳定性和功能都在不断地增强和完善.zabbix拥有详细的UI界面和分组策 ...

  10. Oracle 12C 物理Standby 主备切换switchover

    Oracle 12C 物理Standby 主备切换switchover Oracle 12C 物理Standby 主备切换switchover Table of Contents 1. 简述 2. 切 ...