Arkady owns a non-decreasing array 𝑎1,𝑎2,…,𝑎𝑛. You are jealous of its beauty and want to destroy this property. You have a so-called XOR-gun that you can use one or more times.
In one step you can select two consecutive elements of the array, let’s say 𝑥 and 𝑦, remove them from the array and insert the integer 𝑥⊕𝑦 on their place, where ⊕ denotes the bitwise XOR operation. Note that the length of the array decreases by one after the operation. You can’t perform this operation when the length of the array reaches one.
For example, if the array is [2,5,6,8], you can select 5 and 6 and replace them with 5⊕6=3. The array becomes [2,3,8].
You want the array no longer be non-decreasing. What is the minimum number of steps needed? If the array stays non-decreasing no matter what you do, print −1.
Input
The first line contains a single integer 𝑛 (2≤𝑛≤105) — the initial length of the array.
The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the elements of the array. It is guaranteed that 𝑎𝑖≤𝑎𝑖+1 for all 1≤𝑖<𝑛.
Output
Print a single integer — the minimum number of steps needed. If there is no solution, print −1.
Examples
inputCopy
4
2 5 6 8
outputCopy
1
inputCopy
3
1 2 3
outputCopy
-1
inputCopy
5
1 2 4 6 20
outputCopy
2
分析:考虑解的一般形式,必定是由一段连续的区间产生,且若区间长度为L则答案就应该是L-2,设L最后合并为两个元素a,b 则 a > b,考虑a ^ b 的二进制最高位1,可以知道a的二进制表示中这个位置一定也为1,且b中对应位置一定为0,这就启发我们可以考虑去枚举L,设L的区间异或和为c,对于c的最高位1,如果它在L的某个真前缀出现过,那么这个前缀对应的前缀异或就可以作为上边结论中的a,其对应的后缀就是结论中的b,此时一定有a > b,否则和最高位的假设矛盾,通过这个结论我们可以知道如果固定区间的起始位置,则随着区间长度L的增加新的区间异或合的最高位1如果一旦在前边的某个前缀和中出现过则我们就找到了一组合法解,题目中a[i]最多只有30位所以答案一定不会很大,直接暴力枚举起始为止就可以了。