博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!B. Johnny and His Hobbies(异或)---题解
阅读量:4033 次
发布时间:2019-05-24

本文共 2581 字,大约阅读时间需要 8 分钟。

来源:

Among Johnny’s numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad’s office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.

There is a set S containing very important numbers on his dad’s desk. The minute Johnny heard about it, he decided that it’s a good idea to choose a positive integer k and replace each element s of the set S with s⊕k (⊕ denotes the exclusive or operation).

Help him choose such k that Johnny’s dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn’t matter, i.e. set {1,2,3} equals to set {2,1,3}.

Formally, find the smallest positive integer k such that {s⊕k|s∈S}=S or report that there is no such number.

For example, if S={1,3,4} and k=2, new set will be equal to {3,1,6}. If S={0,1,2,3} and k=1, after playing set will stay the same.

Input

In the first line of input, there is a single integer t (1≤t≤1024), the number of test cases. In the next lines, t test cases follow. Each of them consists of two lines.

In the first line there is a single integer n (1≤n≤1024) denoting the number of elements in set S. Second line consists of n distinct integers si (0≤si<1024), elements of S.

It is guaranteed that the sum of n over all test cases will not exceed 1024.

Output

Print t lines; i-th line should contain the answer to the i-th test case, the minimal positive integer k satisfying the conditions or −1 if no such k exists.

Example

inputCopy
6
4
1 0 2 3
6
10 7 14 8 3 12
2
0 2
3
1 2 3
6
1 4 6 10 11 12
2
0 1023
outputCopy
1
4
2
-1
-1
1023
Note
In the first test case, the answer is 1 because it is a minimum positive integer and it satisfies all the conditions.

题意:

给你一个数组n,让你找出最小的数使得异或数组中每一个数之后,数组中的元素不变。

思路:

暴力就ok…

代码:

#include 
#include
#define maxn 1100using namespace std;int a[maxn], b[maxn];int main() {
int t, n, i, j, flag = 0; cin>>t; while (t--) {
cin>>n; for (i = 1; i <= n; i++) cin>>a[i]; sort(a + 1, a + 1 + n); for (i = 1; i <= 1024; i++) {
flag = 0; for (j = 1; j <= n; j++) b[j] = a[j] ^ i; sort(b + 1, b + 1 + n); for (j = 1; j <= n; j++) if (b[j] != a[j]) {
flag = 1; break; } if (!flag)break; } if (!flag) cout << i << endl; else cout<<"-1\n"; } return 0;}

转载地址:http://fofdi.baihongyu.com/

你可能感兴趣的文章
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>