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
轉載于:https://www.cnblogs.com/Patt/p/11525004.html
總結
以上是生活随笔為你收集整理的Maximum XOR Sum 系列问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【题解】luogu p1111 修复公路
- 下一篇: Fast CGI 工作原理