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

原文地址:https://www.cnblogs.com/Patt/p/11525004.html

时间: 2024-11-09 10:31:40

Maximum XOR Sum 系列问题的相关文章

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

题目来源:Light OJ 1272 Maximum Subset Sum 题意:选出一些数 他们的抑或之后的值最大 思路:每个数为一个方程 高斯消元 从最高位求出上三角 消元前k个a[i]异或和都能有消元后的异或和组成 消元前 k 个 a[i] a[i]异或和都能有消元后的 异或和都能有消元后的 p 个 a[i] a[i]的异或 的异或 保证每一列只有一个1 消元后所有A[i]抑或起来就是答案 #include <cstdio> #include <cstring> #inclu

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Binary Tree Maximum Path Sum 自底向上求解(重重重)

题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,如果左右子树最大路径和小于0,那么返回零, 用这个最大路径和和根节点的值相加,来更新最大值,同时, 更新返回该树的最大路径值. 代码: class Solution { public: int max = INT_MIN; int maxPathSum(TreeNode *root) { if (root == NULL) return 0; search(root); return max;

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

HDU 4825 Xor Sum(经典01字典树+贪心)

Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 1555    Accepted Submission(s): 657 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Ze

Maximum Subsequence Sum - 最大子列和问题_C语言实现

第一次写这方面的blog.自己也是初次接触相关知识,写的有不妥的地方十分欢迎大家指正~ 这是浙大PAT上的一道算法题(据说是浙大04年研究生复试题),题目是这样的: Maximum Subsequence Sum Given a sequence of KK integers { N_1N?1??, N_2N?2??, ..., N_KN?K?? }. A continuous subsequence is defined to be { N_iN?i??, N_{i+1}N?i+1??, ..

[LeetCode][Java] 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. For example: Given the below binary tree, 1 / 2 3 Return 6. 题意: 给定一棵二叉树,找出最大得路径和. 路径的起始和结束位置可以是树中的任意节点. 比如, 给定如下的一棵二叉树 1 / 2 3 返回  6. 算法分析: 1) Rec

LeetCode_Binary Tree Maximum Path Sum

一.题目 Binary Tree Maximum Path Sum Total Accepted: 41576 Total Submissions: 193606My Submissions Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Retu