803_div2(日常垫底- XOR Mixup

A. XOR Mixup

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an array 𝑎a with 𝑛−1n−1 integers. Let 𝑥x be the bitwise XOR of all elements of the array. The number 𝑥x is added to the end of the array 𝑎a (now it has length 𝑛n), and then the elements are shuffled.

You are given the newly formed array 𝑎a. What is 𝑥x? If there are multiple possible values of 𝑥x, you can output any of them.

Input

The input consists of multiple test cases. The first line contains an integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer 𝑛n (2≤𝑛≤1002≤n≤100) — the number of integers in the resulting array 𝑎a.

The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (0≤𝑎𝑖≤1270≤ai≤127) — the elements of the newly formed array 𝑎a.

Additional constraint on the input: the array 𝑎a is made by the process described in the statement; that is, some value of 𝑥x exists.

Output

For each test case, output a single integer — the value of 𝑥x, as described in the statement. If there are multiple possible values of 𝑥x, output any of them.

Example

input

Copy

4
4
4 3 2 5
5
6 1 10 7 10
6
6 6 6 6 6 6
3
100 100 0

output

Copy

3
7
6
0

Note

In the first test case, one possible array 𝑎a is 𝑎=[2,5,4]a=[2,5,4]. Then 𝑥=2⊕5⊕4=3x=2⊕5⊕4=3 (⊕⊕ denotes the bitwise XOR), so the new array is [2,5,4,3][2,5,4,3]. Afterwards, the array is shuffled to form [4,3,2,5][4,3,2,5].

In the second test case, one possible array 𝑎a is 𝑎=[1,10,6,10]a=[1,10,6,10]. Then 𝑥=1⊕10⊕6⊕10=7x=1⊕10⊕6⊕10=7, so the new array is [1,10,6,10,7][1,10,6,10,7]. Afterwards, the array is shuffled to form [6,1,10,7,10][6,1,10,7,10].

In the third test case, all elements of the array are equal to 66, so 𝑥=6x=6.

In the fourth test case, one possible array 𝑎a is 𝑎=[100,100]a=[100,100]. Then 𝑥=100⊕100=0x=100⊕100=0, so the new array is [100,100,0][100,100,0]. Afterwards, the array is shuffled to form [100,100,0][100,100,0]. (Note that after the shuffle, the array can remain the same.)

代码:

#include "bits/stdc++.h"

using namespace std;
const int N = 1e05 + 10, M = N * 2;

int read(){
	int res = 0 , flag = 1 ;
	char c = getchar() ;
	while(!isdigit(c)){
		if(c == '-') flag = -1 ;
		c = getchar() ;
	}
	while(isdigit(c)){
		res = (res << 1) + (res << 3) + (c ^ 48) ;
		c = getchar() ;
	}
	return res * flag ; 
}

inline void solved() {
	int n;
	n = read();
	vector<int> q(n);

	for (int i = 0; i < n; i++) {
		cin >> q[i];
	}

	cout << q[n - 1] << endl;
}
int main() {
	int  _;
	cin >> _;
	while (_ --) {
		solved();
	}

	return 0;
}

This question is actually using A XOR A = 0 to prove that any element in this array can be represented by other elements, so just output one.

Any element of the array works. Let's use ⊕⊕ for 𝖷𝖮𝖱XOR.

Suppose that the original array is [𝑎1,…,𝑎𝑛−1][a1,…,an−1]. Then 𝑥=𝑎1⊕⋯⊕𝑎𝑛−1x=a1⊕⋯⊕an−1.

Let's show that 𝑎1a1 is the 𝖷𝖮𝖱XOR of all other elements of the array; that is, 𝑎1=𝑎2⊕⋯⊕𝑎𝑛−1⊕𝑥a1=a2⊕⋯⊕an−1⊕x. Substituting 𝑥=𝑎1⊕⋯⊕𝑎𝑛−1x=a1⊕⋯⊕an−1, we have

𝑎1=𝑎2⊕⋯⊕𝑎𝑛−1⊕𝑎2⊕⋯⊕𝑎𝑛−1.a1=a2⊕⋯⊕an−1⊕a2⊕⋯⊕an−1.

There are two occurences of 𝑎2,…,𝑎𝑛a2,…,an on the right-hand side and only one occurrence of 𝑎1a1. Since 𝑦⊕𝑦=0y⊕y=0 for all 𝑦y, the 𝑎2,…,𝑎𝑛−1a2,…,an−1 all cancel out, leaving only 𝑎1a1.

More formally, we can write the following.

𝑎1=?𝑎2⊕⋯⊕𝑎𝑛−1⊕𝑥=𝑎2⊕⋯⊕𝑎𝑛−1⊕(𝑎1⊕⋯⊕𝑎𝑛−1)=𝑎2⊕⋯⊕𝑎𝑛−1⊕(𝑎1⊕𝑎2⊕⋯⊕𝑎𝑛−1)=(𝑎2⊕⋯⊕𝑎𝑛−1)⊕𝑎1⊕(𝑎2⊕⋯⊕𝑎𝑛−1)=𝑎1⊕(𝑎2⊕⋯⊕𝑎𝑛−1)⊕(𝑎2⊕⋯⊕𝑎𝑛−1)=𝑎1.a1=?a2⊕⋯⊕an−1⊕x=a2⊕⋯⊕an−1⊕(a1⊕⋯⊕an−1)=a2⊕⋯⊕an−1⊕(a1⊕a2⊕⋯⊕an−1)=(a2⊕⋯⊕an−1)⊕a1⊕(a2⊕⋯⊕an−1)=a1⊕(a2⊕⋯⊕an−1)⊕(a2⊕⋯⊕an−1)=a1.

The same proof follows for any 𝑎𝑖ai. Hence you can output any element of the array.

The time complexity is O(n).

改变XOR的顺序是对于输出结果不会有变化。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值