3164. 线性基

题目链接

3164. 线性基

给定 n 个整数(可能重复),现在需要从中挑选任意个整数,使得选出整数的异或和最大。

请问,这个异或和的最大可能值是多少。

输入格式

第一行一个整数 n

第二行包含 n 个整数。

输出格式

输出一个整数,表示所选整数的异或和的最大可能值。

数据范围

1n105,
0Si2631

输入样例:

3 5 2 8

输出样例:

15

解题思路

线性基

线性基:是向量空间的一组基,通常用来解决异或问题。
即对于一组向量组来说,其线性空间和线性基表示的线性空间等价,当然里面包括了最小/大值等,对于异或线性基,其有如下性质:

  • 线性基没有异或和为 0 的子集

  • 每个存在的最高位只有一个元素

  • 每个元素的异或方案唯一

  • 线性基是线性无关的,即里面的任意元素不能由除本身外的其他元素表示

线性基即极大线性无关组,其维度即向量组的秩,其可用高斯消元求解,一般有两种写法:普通的高斯消元和一个一个插入,其中一个一个插入的形成的线性基不是真正的线性基,例如 a[i] 表示第 i 位为 1 的线性基,其后面表示的元素第 i 位都为 0,而其前面更高位的 j 表示的 a[j]i 位可能为 1,高斯消元得到的 i 位前后当且仅当只有一个线性基元素才为 1

本题要求从中选取的任意异或和最大,先找出线性基,由于线性基里的元素表示的空间和原向量组表示的空间等价,其包含异或和的最大值,而线性基每个元素的最高位唯一,线性基里的所有元素的异或和即为答案
另外求异或和最小值即线性基的里的最小元素,为什么?线性基的元素最高位都是唯一的,最小的元素异或其他元素只会使结果变大。查询一个数是否可以用线性基异或表示,可采用一个一个插入的写法,模仿插入的过程,如果最后插入的数 x=0 说明可以表示出来

  • 时间复杂度:(63n)

代码

  • 写法一
// Problem: 线性基 // Contest: AcWing // URL: https://www.acwing.com/problem/content/3167/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=1e5+5; int n,k; LL a[N],res; int main() { help; cin>>n; for(int i=0;i<n;i++)cin>>a[i]; for(int i=62;i>=0;i--) { for(int j=k;j<n;j++) if(a[j]>>i&1) { swap(a[j],a[k]); break; } if(!(a[k]>>i&1))continue; for(int j=0;j<n;j++) if(j!=k&&(a[j]>>i&1))a[j]^=a[k]; k++; } for(int i=0;i<k;i++)res^=a[i]; cout<<res; return 0; }
  • 写法二
// Problem: 线性基 // Contest: AcWing // URL: https://www.acwing.com/problem/content/3167/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=1e5+5; int n; LL a[65],res,x; void insert(LL x) { for(int i=62;i>=0;i--) if(x>>i&1) { if(!a[i]) { a[i]=x; break; } else x^=a[i]; } } int main() { help; cin>>n; for(int i=0;i<n;i++)cin>>x,insert(x); for(int i=62;i>=0;i--) if(!(res>>i&1))res^=a[i]; cout<<res; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16531862.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示