A12305 | (Zero XOR Subset)-less
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an array $a_1, a_2, \dots, a_n$ 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.
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.
输入格式
The first line contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the size of the array.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 10^9$ ).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 10^9$ ).
输出格式
Print the maximum number of segments the array can be divided into while following the given constraints. Print -1 if no suitable division exists.
输入输出样例
输入 #1
4 5 5 7 2
输出 #1
2
输入 #2
3 1 2 3
输出 #2
-1
输入 #3
3 3 1 10
输出 #3
3
In the first example $2$ is the maximum number. If you divide the array into $\{[5], [5, 7, 2]\}$ , the XOR value of the subset of only the second segment is $5 \oplus 7 \oplus 2 = 0$ . $\{[5, 5], [7, 2]\}$ has the value of the subset of only the first segment being $5 \oplus 5 = 0$ . However, $\{[5, 5, 7], [2]\}$ will lead to subsets $\{[5, 5, 7]\}$ of XOR $7$ , $\{[2]\}$ of XOR $2$ and $\{[5, 5, 7], [2]\}$ of XOR $5 \oplus 5 \oplus 7 \oplus 2 = 5$ .
Let's take a look at some division on $3$ segments — $\{[5], [5, 7], [2]\}$ . It will produce subsets:
- $\{[5]\}$ , XOR $5$ ;
- $\{[5, 7]\}$ , XOR $2$ ;
- $\{[5], [5, 7]\}$ , XOR $7$ ;
- $\{[2]\}$ , XOR $2$ ;
- $\{[5], [2]\}$ , XOR $7$ ;
- $\{[5, 7], [2]\}$ , XOR $0$ ;
- $\{[5], [5, 7], [2]\}$ , XOR $5$ ;
As you can see, subset $\{[5, 7], [2]\}$ has its XOR equal to $0$ , which is unacceptable. You can check that for other divisions of size $3$ or $4$ , non-empty subset with $0$ XOR always exists.
The second example has no suitable divisions.
The third example array can be divided into $\{[3], [1], [10]\}$ . No subset of these segments has its XOR equal to $0$ .
Let's take a look at some division on $3$ segments — $\{[5], [5, 7], [2]\}$ . It will produce subsets:
- $\{[5]\}$ , XOR $5$ ;
- $\{[5, 7]\}$ , XOR $2$ ;
- $\{[5], [5, 7]\}$ , XOR $7$ ;
- $\{[2]\}$ , XOR $2$ ;
- $\{[5], [2]\}$ , XOR $7$ ;
- $\{[5, 7], [2]\}$ , XOR $0$ ;
- $\{[5], [5, 7], [2]\}$ , XOR $5$ ;
As you can see, subset $\{[5, 7], [2]\}$ has its XOR equal to $0$ , which is unacceptable. You can check that for other divisions of size $3$ or $4$ , non-empty subset with $0$ XOR always exists.
The second example has no suitable divisions.
The third example array can be divided into $\{[3], [1], [10]\}$ . No subset of these segments has its XOR equal to $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted