zoukankan      html  css  js  c++  java
  • Codeforces Good Bye 2019 C. Make Good

    原文链接:https://www.cnblogs.com/xwl3109377858/p/12124554.html

    Good Bye 2019

    C. Make Good

    Let's call an array a1,a2,…,am of nonnegative integer numbers good if a1+a2+⋯+am=2⋅(a1⊕a2⊕⋯⊕am), where ⊕denotes the bitwise XOR operation.

    For example, array [1,2,3,6] is good, as 1+2+3+6=12=2⋅6=2⋅(1⊕2⊕3⊕6). At the same time, array [1,2,1,3] isn't good, as 1+2+1+3=7≠2⋅1=2⋅(1⊕2⊕1⊕3).

    You are given an array of length n: a1,a2,…,an. Append at most 3 elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10000). The description of the test cases follows.

    The first line of each test case contains a single integer n (1≤n≤105) — the size of the array.

    The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109) — the elements of the array.

    It is guaranteed that the sum of n over all test cases does not exceed 105.

    Output

    For each test case, output two lines.

    In the first line, output a single integer s (0≤s≤3) — the number of elements you want to append.

    In the second line, output s integers b1,…,bs (0≤bi≤1018) — the elements you want to append to the array.

    If there are different solutions, you are allowed to output any of them.

    Example

    input

    3

    4

    1 2 3 6

    1

    8

    2

    1 1

    output

    0

     

    2

    4 4

    3

    2 6 2

    Note

    In the first test case of the example, the sum of all numbers is 12, and their ⊕ is 6, so the condition is already satisfied.

    In the second test case of the example, after adding 4,4, the array becomes [8,4,4]. The sum of numbers in it is 16, ⊕ of numbers in it is 8.

     

    题意:题意大概是 定义一个序列为好的序列,如果序列的数值总和等于把他们异或起来的和的两倍。 

    然后有T组数据,每组数据给你一个n,和n个数,让你添加小于等于3个元素,使序列变成好的序列。

    思路:我们可以先对所给序列处理,算出总和sum和异或和pre,先特判 sum = pre*2

    本身已经是好的序列就不用添加元素。否则,根据异或的性质,两个相同的数序列异或起来为0,

    那么可以添加两个元素,一个pre,一个sum+pre,这样一定成立。

    因为 pre⊕pre=0,0⊕(sum+pre)=sum+pre 而 sum+pre+(sum+pre) = (sum+pre)*2 得证。

    #include<iostream>
    using namespace std;
    #define ll long long
     
    const int maxn=2e5+10;
     
    int num[maxn];
     
    int main()
    {
        ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
        
        int T;
        cin>>T;
        
        int n;
        while(T--)
        {
            cin>>n;
            
            ll int sum=0,pre=0;
             
            for(int i=0;i<n;i++)
            {
                cin>>num[i];
                sum+=num[i];
                pre^=num[i];
            }
            
            if(sum==(pre<<1))
            {
                cout<<0<<endl;
                cout<<endl;
                continue;
            }
            
            cout<<2<<endl;
            cout<<pre<<" "<<sum+pre<<endl;
            
        }
        
        return 0;
    }
    大佬见笑,,
  • 相关阅读:
    Java大小写转换
    java基础总结第一部分
    7-1 哥尼斯堡的“七桥问题” (25 分)
    关于并查集问题
    L1-049 天梯赛座位分配 (20 分)
    fuser 好命令
    android phone retmote control vlc player
    ubuntu 启动失败 虚惊一场
    useful vim
    cross_compile mtd-utils 出错解决
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/12124554.html
Copyright © 2011-2022 走看看