【LOJ6060】【2017 山东一轮集训 Day1 / SDWC2018 Day1】Set 线性基
题目大意
给出 \(n\) 个非负整数,将数划分成两个集合,记为一号集合和二号集合。\(x_1\) 为一号集合中所有数的异或和,\(x_2\) 为二号集合中所有数的异或和。在最大化 \(x_1 + x_2\) 的前提下,最小化 \(x_1\)。
\(n\leq 100000,0\leq a_i\leq {10}^8\)
题解
记 \(s=a_1\operatorname{xor} a_2\operatorname{xor} a_3\operatorname{xor} \cdots\operatorname{xor}a_n\)。
那么就是要在最大化 \(s\operatorname{xor}x_2+x_2\) 的前提下最大化 \(x_2\)。
如果对于一个二进制位 \(i\),如果 \(s\) 在这一位上的值为 \(0\),并且 \(x_2\) 在这一位上的值为 \(1\),那么就会对 \(s\operatorname{xor}x_2+x_2\) 有 \(2^{i+1}\) 的贡献。
如果对于一个二进制位 \(i\),如果 \(s\) 在这一位上的值为 \(1\),并且 \(x_2\) 在这一位上的值为 \(1\),那么就会对 \(x_2\) 有 \(2^i\) 的贡献。
那么把这些二进制位分成两部分,求线性基,然后随便搞搞贪心取就好了。
时间复杂度:\(O(n\log V)\)
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<functional>
#include<cmath>
//using namespace std;
using std::min;
using std::max;
using std::swap;
using std::sort;
using std::reverse;
using std::random_shuffle;
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
ll rd(){ll s=0;int c,b=0;while(((c=getchar())<'0'||c>'9')&&c!='-');if(c=='-'){c=getchar();b=1;}do{s=s*10+c-'0';}while((c=getchar())>='0'&&c<='9');return b?-s:s;}
void put(int x){if(!x){putchar('0');return;}static int c[20];int t=0;while(x){c[++t]=x%10;x/=10;}while(t)putchar(c[t--]+'0');}
int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;}
int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;}
ll a[100010];
ll s[100];
ll sum;
int n;
void insert(ll v)
{
for(int i=62;i>=0;i--)
if(!((sum>>i)&1)&&((v>>i)&1))
{
if(!s[i])
{
s[i]=v;
return;
}
v^=s[i];
}
for(int i=62;i>=0;i--)
if(((sum>>i)&1)&&((v>>i)&1))
{
if(!s[i])
{
s[i]=v;
return;
}
v^=s[i];
}
}
int main()
{
open("loj6060");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
a[i]=rd();
sum^=a[i];
}
for(int i=1;i<=n;i++)
insert(a[i]);
ll ans=0;
for(int i=62;i>=0;i--)
if(!((sum>>i)&1)&&((ans^s[i])+(sum^ans^s[i])>ans+(sum^ans)||((ans^s[i])+(sum^ans^s[i])==ans+(sum^ans)&&(ans^s[i])>ans)))
ans^=s[i];
for(int i=62;i>=0;i--)
if(((sum>>i)&1)&&((ans^s[i])+(sum^ans^s[i])>ans+(sum^ans)||((ans^s[i])+(sum^ans^s[i])==ans+(sum^ans)&&(ans^s[i])>ans)))
ans^=s[i];
printf("%lld\n",sum^ans);
return 0;
}
【LOJ6060】【2017 山东一轮集训 Day1 / SDWC2018 Day1】Set 线性基的更多相关文章
- LOJ 6060「2017 山东一轮集训 Day1 / SDWC2018 Day1」Set(线性基,贪心)
LOJ 6060「2017 山东一轮集训 Day1 / SDWC2018 Day1」Set $ solution: $ 这一题的重点在于优先级问题,我们应该先保证总和最大,然后再保证某一个最小.于是我 ...
- 【LOJ6067】【2017 山东一轮集训 Day3】第三题 FFT
[LOJ6067][2017 山东一轮集训 Day3]第三题 FFT 题目大意 给你 \(n,b,c,d,e,a_0,a_1,\ldots,a_{n-1}\),定义 \[ \begin{align} ...
- Loj #6069. 「2017 山东一轮集训 Day4」塔
Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...
- Loj 6068. 「2017 山东一轮集训 Day4」棋盘
Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...
- 「2017 山东一轮集训 Day5」苹果树
「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
- 【LOJ#6066】「2017 山东一轮集训 Day3」第二题(哈希,二分)
[LOJ#6066]「2017 山东一轮集训 Day3」第二题(哈希,二分) 题面 LOJ 题解 要哈希是很显然的,那么就考虑哈希什么... 要找一个东西可以表示一棵树,所以我们找到了括号序列. 那么 ...
- loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流
loj6068. 「2017 山东一轮集训 Day4」棋盘 链接 https://loj.ac/problem/6068 思路 上来没头绪,后来套算法,套了个网络流 经典二分图 左边横,右边列 先重新 ...
- LOJ #6074. 「2017 山东一轮集训 Day6」子序列
#6074. 「2017 山东一轮集训 Day6」子序列 链接 分析: 首先设f[i][j]为到第i个点,结尾字符是j的方案数,这个j一定是从i往前走,第一个出现的j,因为这个j可以代替掉前面所有j. ...
- 【LOJ6077】「2017 山东一轮集训 Day7」逆序对 生成函数+组合数+DP
[LOJ6077]「2017 山东一轮集训 Day7」逆序对 题目描述 给定 n,k ,请求出长度为 n的逆序对数恰好为 k 的排列的个数.答案对 109+7 取模. 对于一个长度为 n 的排列 p ...
随机推荐
- .Net Core + Angular Cli / Angular4 开发环境搭建
一.基础环境配置 1.安装VS 2017 v15.3或以上版本 2.安装VS Code最新版本 3.安装Node.js v6.9以上版本 4.重置全局npm源,修正为 淘宝的 NPM 镜像: npm ...
- js 数组去重小技巧
js 数组去重小技巧 Intro 今天遇到一个问题,需要对数据进行去重,想看一下有没有什么比较方便的方法,果然有些收获. Question 问题描述: 我有一个这样的数据: [ { "Pro ...
- python2和python3的一些差别
未来python3是主流,但还是因为一部分原因,很多项目还在用python2.python3是不兼容python2的,做了一些优化和改进 1.字符编码 python2:ascii编码 python3: ...
- webmagic 爬取网页所有文章的标题时间作者和内容
package com.ij34; import us.codecraft.webmagic.Site; import us.codecraft.webmagic.Page; import us.co ...
- SQLServer之创建标量函数
创建标量函数注意事项 在 SQL Server 和 Azure SQL Database 中创建用户定义函数. 用户定义函数是接受参数.执行操作(例如复杂计算)并将操作结果以值的形式返回的 Trans ...
- c/c++浮点数在内存中存储方式
转自:https://www.cnblogs.com/dolphin0520/archive/2011/10/02/2198280.html 任何数据在内存中都是以二进制的形式存储的,例如一个shor ...
- Docker: Harbor一些小知识
镜像文件上传到私有仓库harbor后,镜像的物理存储位置在哪里? 这些信息记录在docker-compose.yml里,通过观察发现 镜像存储在了宿主机的 volumes: - /data/regis ...
- 推荐六款炫酷的HTML5效果插件
1. HTML5 3D图片阴影翻转动画 效果很酷 分享一款很酷的HTML5 3D动画特效,这款3D特效可以为你的图片增加阴影的效果,而且可以让图片在鼠标滑过的时候出现3D翻转的动画效果.这和HTML5 ...
- 推荐一套Angular2的UI模板
Core UI Core UI是一款基于Bootstrap4的UI模板,有html.angular2,react和vue版.我是在使用angular2版本中发现其项目结构不符合angular风格指南推 ...
- Python爬虫【实战篇】百度贴吧爬取页面存到本地
先上代码 import requests class TiebaSpider: def __init__(self, tieba_name): self.tieba_name = tieba_name ...