ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基

2019-02-02 20:51:16  阅读:391  来源: 互联网

标签:Subset subset XOR less segments 异或 array Copy


题目传送门

题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为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+10;
ll a[maxn],p[40],s[maxn];
int n;
int add(ll val){
    for(int i=30;i>=0;i--)
    {
        if(val&(1<<i)){
            if(!p[i]){
                p[i]=val;
                return 1;
            }
            val^=p[i];
        }
    }
    return 0;
}
int main(){
    while(cin>>n)
    {
        clr(p,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            s[i]=s[i-1]^a[i];
        }
        if(s[n]==0){
            puts("-1");
            continue;
        }
        int ans=0;
        for(int i=n;i>0;i--)
        {
            ans+=add(a[i]);
        }
        cout<<ans<<endl;
    }
} 
View Code

 

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.

标签:Subset,subset,XOR,less,segments,异或,array,Copy
来源: https://www.cnblogs.com/mountaink/p/10349112.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有