codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基
题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为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;
}
}
2 seconds
256 megabytes
standard input
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.
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).
Print the maximum number of segments the array can be divided into while following the given constraints. Print -1 if no suitable division exists.
4
5 5 7 2
2
3
1 2 3
-1
3
3 1 10
3
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 前缀异或+线性基的更多相关文章
- 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 ...
- bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...
- Codeforces 895C Square Subsets(状压DP 或 异或线性基)
题目链接 Square Subsets 这是白书原题啊 先考虑状压DP的做法 $2$到$70$总共$19$个质数,所以考虑状态压缩. 因为数据范围是$70$,那么我们统计出$2$到$70$的每个数的 ...
- BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)
题目链接 BZOJ 4568 考虑树链剖分+线段树维护每一段区域的异或线性基 对于每个询问,求出该点集的异或线性基.然后求一下这个线性基里面能异或出的最大值即可. #include <bits ...
- 2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )
题目链接 2017西安赛区 Problem A 题意 给定一个数列,和$q$个询问,每个询问中我们可以在区间$[L, R]$中选出一些数. 假设我们选出来的这个数列为$A[i_{1}]$, $A[ ...
- LOJ2312 LUOGU-P3733「HAOI2017」八纵八横 (异或线性基、生成树、线段树分治)
八纵八横 题目描述 Anihc国有n个城市,这n个城市从1~n编号,1号城市为首都.城市间初始时有m条高速公路,每条高速公路都有一个非负整数的经济影响因子,每条高速公路的两端都是城市(可能两端是同一个 ...
- 前缀和线性基HDU6579
Operation 题解:看到区间最大异或和,首先想到的是线性基: 线性基可以处理的操作是: 在数列末尾插入一个数 查询全局的子集异或最大值 由于线性基的长度很短,因此我们可以将数列所有前缀的线性基保 ...
- 【2017西安邀请赛:A】XOR(线段树+线性基)
前言:虽然已经有很多题解了,但是还是想按自己的理解写一篇. 思路:首先分析题目 一.区间操作 —— 线段树 二.异或操作 —— 线性基 这个两个不难想,关键是下一步的技巧 “或”运算 就是两个数的二进 ...
- XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...
随机推荐
- ubuntu PCL的使用
cmake_minimum_required(VERSION 2.8) project(MY_GRAND_PROJECT) find_package(PCL 1.3 REQUIRED COMPONEN ...
- logistic growth model . 求解方法 (高斯牛顿法)
https://www.stat.tamu.edu/~jnewton/604/chap4.pdf http://www.metla.fi/silvafennica/full/sf33/sf334327 ...
- Spring下面的@Transactional注解标志的讲解
最近在开发中对Spring中的事务标记@Transactional用的比较多,今天上网收集了一些内容,做一个简单的总结~~~ 在service类前加上@Transactional,声明这个servic ...
- Web API集成Azure AD认证
1.声明的介绍 基于角色的授权管理,适用于角色变化不大,并且用户权限不会频繁更改的场景. 在更复杂的环境下,仅仅通过给用户分配角色并不能有效地控制用户访问权限. 基于声明的授权有许多好处,它使认证和授 ...
- Java 数据结构之数组
public class Arrays { //创建一个Integer空数组 public static Integer[] player=null; //添加球员号码 pri ...
- 【Head First Java 读书笔记】(二)类与对象
前篇当中,代码都放在main()里面,那根本不是面向对象的做法. 椅子大战(对象如何改变你的一生) 程序规格: 在图形接口画出四方形,圆形和三角形,当用户点选图形时,图形需要顺时针转360度并依据形状 ...
- .net 基元类型,引用类型和值类型
基元类型(primitive type): 编译器直接支持的数据类型称为基元类型(primitive type). string 与 String: 由于C#中的string (一个关键字)直接映射到 ...
- 基于任务的异步编程模式(TAP)的错误处理
在前面讲到了<基于任务的异步编程模式(TAP)>,但是如果调用异步方法,没有等待,那么调用异步方法的线程中使用传统的try/catch块是不能捕获到异步方法中的异常.因为在异步方法执行出现 ...
- Vue watch用法
Vue.js 提供了一个方法 watch,它用于观察Vue实例上的数据变动.对应一个对象,键是观察表达式,值是对应回调.值也可以是方法名,或者是对象,包含选项. 例如,同一个组件切换时,不会触发生命周 ...
- 使用Linq读取资源文件
ResXResourceReader resxr = new ResXResourceReader(txt_WebResourceOpenFile.Text); IEnumerable<Dict ...