Maximum XOR Sum 系列问题
给定
Problem 1(经典问题)
求
解法
字典树
Problem 2
从
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
and .) 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
Consider painting at least one and at most
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.
解法
此问题可转化为 Problem 2。
若第
我们只需要考虑共有偶数个 1 的那些二进制位,在这些位上,不论如何划分,两部分的异或和一定是相等的,因此我们的目标是使这些位上的异或和最大,于是问题转化为 Problem 2。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
· 用 C# 插值字符串处理器写一个 sscanf