日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maximum XOR Sum 系列问题

發布時間:2024/4/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 系列问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。