【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 ...
随机推荐
- 【20190405】JavaScript-正则式匹配与替换结果解析
在正则式的应用中有三个函数使用得最多:exec().test()与字符串的replace(reg, options).其中test()最简单,只要字符串与正则式可以匹配,就返回true,否则返回fal ...
- arcgis api 3.x for js 入门开发系列十二地图打印GP服务(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- OpenCL中的half与float的转换
在kernel中使用half类型可以在牺牲一定精度的代价下来提升运算速度. 在kernel中, 可以比较方便的对half数据进行计算, 但在host上的, 对half的使用就没那么方便了. 查看cl_ ...
- ionic3 Loading组件的用法
import { LoadingController } from 'ionic-angular'; @Component({ selector: 'page-contact', templateUr ...
- Fragment与Activity的生命周期对比
因为fragment是依赖于activity的,所以activity的创建相关都是先于fragment的,fragment的销毁相关都是先于activity的.
- Android 中使用 dlib+opencv 实现动态人脸检测
1 概述 完成 Android 相机预览功能以后,在此基础上我使用 dlib 与 opencv 库做了一个关于人脸检测的 demo.该 demo 在相机预览过程中对人脸进行实时检测,并将检测到的人脸用 ...
- 微信小程序转发微信小程序转发
微信小程序转发涉及以下4个方法: 1.Page.onShareAppMessage({}) 设置右上角“转发”配置,及转发后回调函数返回 shareTicket 票据 2.wx.showSahreMe ...
- Cmder--Windows下代替原生的cmd命令行工具
Cmder是Windows下的命令行工具,用来代替Windows自带的cmd 官网:http://cmder.net/ 这里下载Full版本 https://github.com/cmderdev/c ...
- ansible学习基础知识和模块(一)
基础知识补充: 常用自动化运维工具 Ansible:使用python来开发的,无需设置Agentless(代理),一般管理几百台.与ssh的方式也不一样,ssh是基于c/s模式(客户端+服务器)来使用 ...
- spark-2.4.0-hadoop2.7-高可用(HA)安装部署
1. 主机规划 主机名称 IP地址 操作系统 部署软件 运行进程 备注 mini01 172.16.1.11[内网] 10.0.0.11 [外网] CentOS 7.5 Jdk-8.zookeepe ...