题目传送门

题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为0.

思路:先处理出前缀异或,这样选择更多的区间其实就相当于选择更多的前缀异或,并且这些前缀异或不能异或出0,这就变成了线性基的基础题了。贪心的放,能放就放。不能放就意味着线性基的add函数里面的val最后变成了0,也就是当前已经插入的线性基已经可以异或出正在插入的数了,所以不能放。

(今天真巧,一连遇到两道线性基的题目)

#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=2e5+;
ll a[maxn],p[],s[maxn];
int n;
int add(ll val){
for(int i=;i>=;i--)
{
if(val&(<<i)){
if(!p[i]){
p[i]=val;
return ;
}
val^=p[i];
}
}
return ;
}
int main(){
while(cin>>n)
{
clr(p,);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
s[i]=s[i-]^a[i];
}
if(s[n]==){
puts("-1");
continue;
}
int ans=;
for(int i=n;i>;i--)
{
ans+=add(a[i]);
}
cout<<ans<<endl;
}
}
G. (Zero XOR Subset)-less
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a1,a2,…,ana1,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 00.

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 nn (1≤n≤2⋅1051≤n≤2⋅105) — the size of the array.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤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
input

Copy
4
5 5 7 2
output

Copy
2
input

Copy
3
1 2 3
output

Copy
-1
input

Copy
3
3 1 10
output

Copy
3
Note

In the first example 22 is the maximum number. If you divide the array into {[5],[5,7,2]}{[5],[5,7,2]}, the XOR value of the subset of only the second segment is 5⊕7⊕2=05⊕7⊕2=0. {[5,5],[7,2]}{[5,5],[7,2]} has the value of the subset of only the first segment being 5⊕5=05⊕5=0. However, {[5,5,7],[2]}{[5,5,7],[2]} will lead to subsets {[5,5,7]}{[5,5,7]} of XOR 77, {[2]}{[2]} of XOR 22 and {[5,5,7],[2]}{[5,5,7],[2]} of XOR 5⊕5⊕7⊕2=55⊕5⊕7⊕2=5.

Let's take a look at some division on 33 segments — {[5],[5,7],[2]}{[5],[5,7],[2]}. It will produce subsets:

  • {[5]}{[5]}, XOR 55;
  • {[5,7]}{[5,7]}, XOR 22;
  • {[5],[5,7]}{[5],[5,7]}, XOR 77;
  • {[2]}{[2]}, XOR 22;
  • {[5],[2]}{[5],[2]}, XOR 77;
  • {[5,7],[2]}{[5,7],[2]}, XOR 00;
  • {[5],[5,7],[2]}{[5],[5,7],[2]}, XOR 55;

As you can see, subset {[5,7],[2]}{[5,7],[2]} has its XOR equal to 00, which is unacceptable. You can check that for other divisions of size 33 or 44, non-empty subset with 00 XOR always exists.

The second example has no suitable divisions.

The third example array can be divided into {[3],[1],[10]}{[3],[1],[10]}. No subset of these segments has its XOR equal to 00.

codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基的更多相关文章

  1. 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 ...

  2. bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...

  3. Codeforces 895C Square Subsets(状压DP 或 异或线性基)

    题目链接  Square Subsets 这是白书原题啊 先考虑状压DP的做法 $2$到$70$总共$19$个质数,所以考虑状态压缩. 因为数据范围是$70$,那么我们统计出$2$到$70$的每个数的 ...

  4. BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)

    题目链接  BZOJ 4568 考虑树链剖分+线段树维护每一段区域的异或线性基 对于每个询问,求出该点集的异或线性基.然后求一下这个线性基里面能异或出的最大值即可. #include <bits ...

  5. 2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )

    题目链接  2017西安赛区 Problem A 题意  给定一个数列,和$q$个询问,每个询问中我们可以在区间$[L, R]$中选出一些数. 假设我们选出来的这个数列为$A[i_{1}]$, $A[ ...

  6. LOJ2312 LUOGU-P3733「HAOI2017」八纵八横 (异或线性基、生成树、线段树分治)

    八纵八横 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路的两端都是城市(可能两端是同一个 ...

  7. 前缀和线性基HDU6579

    Operation 题解:看到区间最大异或和,首先想到的是线性基: 线性基可以处理的操作是: 在数列末尾插入一个数 查询全局的子集异或最大值 由于线性基的长度很短,因此我们可以将数列所有前缀的线性基保 ...

  8. 【2017西安邀请赛:A】XOR(线段树+线性基)

    前言:虽然已经有很多题解了,但是还是想按自己的理解写一篇. 思路:首先分析题目 一.区间操作 —— 线段树 二.异或操作 —— 线性基 这个两个不难想,关键是下一步的技巧 “或”运算 就是两个数的二进 ...

  9. XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)

    题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...

随机推荐

  1. 张超超OC基础回顾_05 property修饰符,id类型,instancetype。。。

    一.property 如果给一个属性同时提供了getter/setter方法, 那么我们称这个属性为可读可写属性 如果只提供了getter方法, 那么我们称这个属性为只读属性 如果只提供了setter ...

  2. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  3. 跨平台的图形软件Dia

    一款非常不错的软件Dia,软件很小,免费.好用.跨平台(linux.windows.mac).可导出多种格式图片,除了流程图.UML建模图,还可以绘制其他很多图. ubuntu下可以直接通过命令行su ...

  4. SpringMVC——RequestMapping

    一.@RequestMapping 映射请求 Spring MVC 通过@RequestMapping注解可以定义不同的处理器映射规则. @RequestMapping放在类名上边,设置请求前缀 方法 ...

  5. UVa 10245 The Closest Pair Problem (分治)

    题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...

  6. easyUI Methods

    doc对象转jQuery 对象 $(doc Object); jQuery Object.控件名('方法'[,参数]); options 为该控件的属性 方式一: var opts = $('.eas ...

  7. 关于在datepicker中,只选年月

    有这么个需求,datepicker默认是选某个具体的日子的,但是现在只选到年月为止, solution: html如下: <div> <label for="startDa ...

  8. web开发四个作用域

    web开发一共有四个作用域,范围从高到低分为appliaction作用域(全局作用域),session作用域,request作用域和page作用域.${base}是el表达式语法,它会自动先从page ...

  9. java实现wc功能

    github项目地址:https://github.com/3216004717/ruanjiangongcheng.git 项目相关要求 基本要求 wc.exe -c file.c //返回文件 f ...

  10. java设计模式 策略

    什么是策略设计模式? 世界永远都在变,唯一不变的就是变本身 举个生活中的例子,小时候玩的游戏中,Sony的PSP提供了统一的卡槽接口,玩家只要更换卡带就可以达到更换游戏的目的,做到了一机多用 特工执行 ...