Challenge From XOR

Mr. AngelClover just learnt XOR on his Computer Class. XOR is a bit arithmetic operator that returns one value if one, but not both, of its operands is one. It is also called Exclusive-OR. Now Mr. AngelClover wonders if it is possible to pick up some numbers from a certain number set, such that the result of Exclusive-OR is zero. And he wants the numbers to be as few as possible but there should be at least one. Can you help him?

 

Input     (Please use standard input, and don’t read or write files.)

The first line is a positive integer N, which means the number of the number set. And there are N lines followed, in each there is a single positive integer, which is the number in the set. Each number in the set is an integer between 0 and 216-1 inclusive.

0< N <=30

 

Output   (Please use standard output, and don’t read or write files.)

There is only one line in the output, which is the smallest number as described above. If Mr. AngelClover can NOT find such numbers after XOR is zero, output -1.

 

Sample Input

Sample Output

4

1

2

3

4

 

3

 

Hint

Numbers which are picked up are 1, 2, 3.

For 40% input data cases, N <= 15.

 

先将输入的数字按从大到小排序,然后逐步地缩小范围,例如本题:

排序后:4     3     2     1,二进制序列为100  11   10   1,对这四个数求XOR,不为0,于是将拥有最高位的数去掉,即去掉4。然后,存在的数有3  2     1,其二进制为11 10   1,求XOR0,于是终止程序,得到结果。

其实,以上的说法是不对的。

并不是去掉了大的数就可以使小的数可以XOR0。例如:7     5     2     1,这个时候反而要去掉1

于是,重写了一下代码,编程最痛苦的事情就在于此啊。

首先,研究一下把即给的数据XOR一下会是什么结果:

100 XOR 10 XOR 11 XOR 1 = 100

发现把即给的所有数XOR的结果是那个要去掉的数100

但是,上述的说法还是不对,要想得更远一些。其实,把所有数XOR的结果是要去掉的所有数的XOR的结果。

编写一个函数,用于控制去掉的顺序是按从1个开始到n个。在去掉j0<j<n+1)个时,必须穷举所有情况,比如去掉1个时就是把所有的数都依次去掉一次,因此有j!次运算。总的运算次数就是j!(0<j<n+1)的和。

当发现某次运算中,去掉的数的XOR等于题给所有数的XOR时,程序得到结果。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值