传送门

题意:

给出\(n\)个数\(a_i\),现在要将其分为两堆,使得这两堆数的异或和相加最大。

思路:

  • 考虑线性基贪心求解。
  • 但直接上线性基求出一组的答案是行不通的,原因之后会说。
  • 注意到如果二进制中某一位\(1\)的个数出现了奇数次,那么无论怎么分,都会有一组中这位为\(1\);对于出现偶数次的位,两组中该位都可以有\(1\),或者都没有\(1\)。
  • 那么我们只需要贪心地插入二进制\(1\)的个数为偶数的那些位就行了,显然这样能使得最终答案最大。

下面口胡一下为什么不能直接用线性基来搞:

如果贪心地利用线性基直接求出一组答案,假设第\(i\)位二进制出现次数为奇数,那么我们可能就以\(i\)为基底,那么其余偶数位作为基底的"可能性"就降低了,所以我们在插入线性基的时候要避免奇数个数的位,这样能使答案最大。

#include <bits/stdc++.h>
#define fi first
#define se second
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5; int n;
ll a[N], p[62];
bool chk[62]; void insert(ll x) {
for(int i = 60; i >= 0; i--) {
if(chk[i]) continue;
if(x >> i & 1) {
if(!p[i]) {
p[i] = x;
break;
}
x ^= p[i];
}
}
} int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
ll all = 0;
for(int i = 1; i <= n; i++) cin >> a[i], all ^= a[i];
for(int i = 0; i <= 60; i++) {
if(all >> i & 1) chk[i] = 1;
}
for(int i = 1; i <= n; i++) insert(a[i]);
ll ans = 0;
for(int i = 0; i <= 60; i++) {
if(chk[i])
for(int j = 0; j <= 60; j++) {
if(p[j] >> i & 1) {
p[j] ^= (1ll << i);
}
}
}
for(int i = 60; i >= 0; i--) {
if((p[i] ^ ans) > ans) ans = p[i] ^ ans;
}
cout << ans + (ans ^ all);
return 0;
}

P.S:实现的话可以一开始就将\(a\)数组\(chk\)了的位的值减去,就让这些位不参与运算,写起来能更加简洁。

如下:

Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5; int n;
ll a[N], p[62]; void insert(ll x) {
for(int i = 60; i >= 0; i--) {
if(x >> i & 1) {
if(!p[i]) {
p[i] = x;
break;
}
x ^= p[i];
}
}
} int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
ll all = 0;
for(int i = 1; i <= n; i++) cin >> a[i], all ^= a[i];
for(int i = 0; i <= 60; i++) {
if(all >> i & 1) {
for(int j = 1; j <= n; j++) {
if(a[j] >> i & 1) a[j] -= (1ll << i);
}
}
}
for(int i = 1; i <= n; i++) insert(a[i]);
ll ans = 0;
for(int i = 60; i >= 0; i--) {
if((p[i] ^ ans) > ans) ans = p[i] ^ ans;
}
cout << ans + (ans ^ all);
return 0;
}

AtCoder abc 141 F - Xor Sum 3(线性基)的更多相关文章

  1. Atcoder ABC 141

    Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #inc ...

  2. CodeForces - 1101G :(Zero XOR Subset)-less(线性基)

    You are given an array a1,a2,…,an of integer numbers. Your task is to divide the array into the maxi ...

  3. [WC2011]最大XOR和路径 线性基

    [WC2011]最大XOR和路径 LG传送门 需要充分发掘经过路径的性质:首先注意不一定是简单路径,但由于统计的是异或值,重复走是不会被统计到的,考虑对于任意一条从\(1\)到\(n\)的路径的有效部 ...

  4. CF1101G (Zero XOR Subset)-less 线性基

    传送门 既然每一次选择出来的都是一个子段,不难想到前缀和计算(然而我没有想到--) 设异或前缀和为\(x_i\),假设我们选出来的子段为\([1,i_1],(i_1,i_2],...,(i_{k-1} ...

  5. 洛谷P4151 [WC2011] 最大XOR和路径 [线性基,DFS]

    题目传送门 最大XOR和路径 格式难调,题面就不放了. 分析: 一道需要深刻理解线性基的题目. 好久没打过线性基的题了,一开始看到这题还是有点蒙逼的,想了几种方法全被否定了.还是看了大佬的题解才会做的 ...

  6. [luogu4151 WC2011] 最大XOR和路径 (线性基)

    传送门 输入输出样例 输入样例#1: 5 7 1 2 2 1 3 2 2 4 1 2 5 1 4 5 3 5 3 4 4 3 2 输出样例#1: 6 说明 [样例说明] 根据异或的性质,将一个数异或两 ...

  7. 2019年牛客多校第四场 B题xor(线段树+线性基交)

    题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...

  8. 牛客练习赛26 D xor序列 (线性基)

    链接:https://ac.nowcoder.com/acm/contest/180/D 来源:牛客网 xor序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...

  9. Wannafly Winter Camp 2020 Day 5J Xor on Figures - 线性基,bitset

    有一个\(2^k\cdot 2^k\) 的全零矩阵 \(M\),给出 \(2^k\cdot 2^k\) 的 \(01\) 矩阵 \(F\),现在可以将 \(F\) 的左上角置于 \(M\) 的任一位置 ...

随机推荐

  1. 使用 IDEA 翻译插件

    使用 IDEA 翻译插件 1.安装 在IDEA插件中搜索 translation根据下载量排序有个完全匹配名称的插件,下载,重启 2.配置翻译插件 都是中文,就不说了

  2. template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)

    org.thymeleaf.exceptions.TemplateInputException: Error resolving template [code/leading], template m ...

  3. day50_9_11 bootstarp使用

    一.下载官网. https://www.bootcss.com/ 使用3版本 https://www.bootcdn.cn/ CDN搜索网址. 配置代码: <link rel="sty ...

  4. vue中的router和route有什么区别?

    我只知道前者一般用在跳转路由的时候,push一个url, 而后者则用来存储路由跳转过程中存储的各种数据. 话不多说,这篇博客讲的比较详细,可以参考一下. vue2.0中的$router 和 $rout ...

  5. angular设置全局变量,修改监听变量

    创建service.module.ts import { NgModule, ModuleWithProviders } from '@angular/core'; import { SomeShar ...

  6. 【2019.7.26 NOIP模拟赛 T3】化学反应(reaction)(线段树优化建图+Tarjan缩点+拓扑排序)

    题意转化 考虑我们对于每一对激活关系建一条有向边,则对于每一个点,其答案就是其所能到达的点数. 于是,这个问题就被我们搬到了图上,成了一个图论题. 优化建图 考虑我们每次需要将一个区间向一个区间连边. ...

  7. 阻止iOS Web APP中点击链接跳转到Safari 浏览器新标签页

    问题:ios封装完之后,点击里边的按钮会跳转到网页上 ——小卡遇到这个问题就是这样解决的↓↓↓ 解决方法:建议将代码放到</head>标签前,当然,另外存为一个js 文件引用也是可以的呦~ ...

  8. [2019BUAA软工助教]助教学期总结

    [2019BUAA软工助教]助教学期总结 一.量化自评 线上 博客点评:https://www.cnblogs.com/ChildishChange/MyComments.html 共 106 条 博 ...

  9. Debug 路漫漫-12:Python: ValueError: 'userid' is both an index level and a column label, which is ambiguous.

    啊,又遇到难题了 == 想要对两个 dataframe 做自然连接 merge,连接的key 为 “userid”,但是报错:ValueError: 'userid' is both an index ...

  10. LCM Walk HDU - 5584

    A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...