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 maximum number of segments in such a way that:
- each element is contained in exactly one segment;
- each segment contains at least one element;
- there doesn't exist a non-empty subset of segments such that bitwise XOR of the numbers from them is equal to 0
- .
Print the maximum number of segments the array can be divided into. Print -1 if no suitable division exists.
Input
The first line contains a single integer n
(1≤n≤2⋅105
) — the size of the array.
The second line contains n
integers a1,a2,…,an (0≤ai≤109
).
Output
Print the maximum number of segments the array can be divided into while following the given constraints. Print -1 if no suitable division exists.
Examples
4
5 5 7 2
2
3
1 2 3
-1
3
3 1 10
3
题意:给你一些树,让你给树分组,然后把每一个分组看成元素,满足没有元素集合的异或和位0。
思路:首先知道一点,我们得到线性基的时候,当1的个数num(基底个数)不等于元素个数时,表示可以拼凑出0 ;那么,如果全部数的异或为0,那么无解;否则,我们现在得到了线性基,最多划分为几个集合呢,答案就是num。 因为这num个位置的最高位1的位置不同,他们是线性无关的。
(给出N个数,要从中选出一个最大的子集,使得子集中的任意个元素异或值不为0,答案也是基底
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
int p[],ans;
int main()
{
int N,sum=,num=,x;
scanf("%d",&N);
rep(i,,N) {
scanf("%d",&x); sum^=x;
rep2(j,,){
if(x&(1LL<<j)){
if(p[j]) x^=p[j];
else { p[j]=x;break;}
}
}
}
if(sum==) return puts("-1"),;
rep(i,,) if(p[i]){
p[num++]=p[i];
rep(j,i+,) if((p[j]>>i)&) p[j]^=p[i];
}
printf("%d\n",num);
return ;
}
CodeForces - 1101G :(Zero XOR Subset)-less(线性基)的更多相关文章
- codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基
题目传送门 题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为0. 思路:先处理出前缀异或,这样 ...
- CF1101G (Zero XOR Subset)-less 线性基
传送门 既然每一次选择出来的都是一个子段,不难想到前缀和计算(然而我没有想到--) 设异或前缀和为\(x_i\),假设我们选出来的子段为\([1,i_1],(i_1,i_2],...,(i_{k-1} ...
- (Zero XOR Subset)-less-线性基
(Zero XOR Subset)-less 题意 :把n个数分成多个集合,要求 不能有集合为空,最终不能有非空子集合异或值为0,尽可能划分的多一些. 思路 :非法情况就只有 n个数异或 为0,其他的 ...
- Codeforces 1299D - Around the World(线性基+图论+dp)
Codeforces 题目传送门 & 洛谷题目传送门 一道线性基的综合题 %%%%%% 首先注意到"非简单路径""异或和"等字眼,可以本能地想到线性基. ...
- Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理
https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...
- [WC2011]最大XOR和路径 线性基
[WC2011]最大XOR和路径 LG传送门 需要充分发掘经过路径的性质:首先注意不一定是简单路径,但由于统计的是异或值,重复走是不会被统计到的,考虑对于任意一条从\(1\)到\(n\)的路径的有效部 ...
- 洛谷P4151 [WC2011] 最大XOR和路径 [线性基,DFS]
题目传送门 最大XOR和路径 格式难调,题面就不放了. 分析: 一道需要深刻理解线性基的题目. 好久没打过线性基的题了,一开始看到这题还是有点蒙逼的,想了几种方法全被否定了.还是看了大佬的题解才会做的 ...
- 牛客练习赛26 D xor序列 (线性基)
链接:https://ac.nowcoder.com/acm/contest/180/D 来源:牛客网 xor序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...
- [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 说明 [样例说明] 根据异或的性质,将一个数异或两 ...
随机推荐
- SpringBoot启动器
pom.xml文件1.父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId ...
- oracle create tablespace
ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED; sqlplus shybt/shybt@127.0.0.1:1521/orcl Cr ...
- vue项目如何打包前后端不分离发布手把手教学apache、nginx
vue项目如何不分离发布 1.首先yarn build 我用了vue-cli脚手架,bulid后的dist文件夹里的index.html有加版本号,那么为什么需要加版本号呢? a.回滚 b.解决浏览器 ...
- 【框架】selenium运行失败后自动截图(三)
思路: 1.写一个类继承TestListenerAdapter,覆写onTestFailure()方法 2.在onTestFailure方法里,调用selenium的TakesScreenShot的g ...
- POJ 1840 Eqs 解方程式, 水题 难度:0
题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...
- commons-logging 与log4j的关系
参考:http://zachary-guo.iteye.com/blog/361177
- 【转】MySQL实现Oracle里的 rank()over(ORDER BY) 功能
Oracle rank()和dense_rank()的区别是: –rank()是跳跃排序,有两个第二名时接下来就是第四名 –dense_rank()l是连续排序,有两个第二名时仍然跟着第三名 sele ...
- Oracle与MySQL的SQL语句区别
2 表 2.1 创建表(同) create table tableName( columnName1 int, columnName2 int ) 2.2 删除表(异) MySQL: drop tab ...
- Linux3.10.0块IO子系统流程(3)-- SCSI策略例程
很长时间以来,Linux块设备使用了一种称为“蓄流/泄流”(plugging/unplugging)的技术来改进吞吐率.简单而言,这种工作方式类似浴盆排水系统的塞子.当IO被提交时,它被储存在一个队列 ...
- oracle插入数据问题
这个是我的表结构:desc T_STUDENT;Name Type Nullable Default Comments ------------ ----------- ...