• 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.)

    代码:

    1. #include "bits/stdc++.h"
    2. using namespace std;
    3. const int N = 1e05 + 10, M = N * 2;
    4. int read(){
    5. int res = 0 , flag = 1 ;
    6. char c = getchar() ;
    7. while(!isdigit(c)){
    8. if(c == '-') flag = -1 ;
    9. c = getchar() ;
    10. }
    11. while(isdigit(c)){
    12. res = (res << 1) + (res << 3) + (c ^ 48) ;
    13. c = getchar() ;
    14. }
    15. return res * flag ;
    16. }
    17. inline void solved() {
    18. int n;
    19. n = read();
    20. vector<int> q(n);
    21. for (int i = 0; i < n; i++) {
    22. cin >> q[i];
    23. }
    24. cout << q[n - 1] << endl;
    25. }
    26. int main() {
    27. int _;
    28. cin >> _;
    29. while (_ --) {
    30. solved();
    31. }
    32. return 0;
    33. }

    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的顺序是对于输出结果不会有变化。

  • 相关阅读:
    网络-navigator.sendBeacon
    大数据毕业设计选题推荐-智慧消防大数据平台-Hadoop-Spark-Hive
    MyBatis-plus超神用法--一文带你玩转MP
    【蓝桥杯真题练习】STEMA科技素养练习题库 答案版009 持续更新中~
    【教学类-15-01】20221115《学号(姓名)描字帖-A4横版-竖切》(中班)
    Ansible-palybook学习
    2023版 STM32实战10 内部Flash读写
    Day980.OAuth 2.0实现一个OpenID Connect用户身份认证协议 -OAuth 2.0
    MYSQL------从概述到DQL
    壳聚糖-聚乙二醇-叠氮,叠氮-PEG-壳聚糖,Chitosan-PEG-N3
  • 原文地址:https://blog.csdn.net/beloved_yu/article/details/125513828