Good Bye 2019 C - Make Good(异或)

https://codeforces.com/contest/1270/problem/C

一个好数组的定义为 a1+a2+⋯+am =2⋅(a1⊕a2⊕⋯⊕am),其中⊕表示按位异或运算。

给你一个长度为n的数组:a1,a2,…,an。最多添加3个元素。

追加的元素不必不同。可以证明在给定的约束条件下,解总是存在的。

如果有不同的解决方案,您可以输出其中任何一个。注意,你不必最小化添加元素的数量!

所以,如果一个数组已经很好了,你可以不追加元素。
input
3
4
1 2 3 6
1
8
2
1 1
output
0

2
4 4
3
2 6 2

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL sum=0;
        LL ans;
        for(LL i=1;i<=n;i++)
        {
            cin>>a[i];
            if(i==1) ans=a[i];
            else ans^=a[i];
            sum+=a[i];
        }
        if(sum==2*ans) cout<<"0"<<endl<<endl;
        else
        {
            cout<<"2"<<endl;
            cout<<ans<<" "<<ans+sum<<endl;
        }
    }
    return 0;
}
posted @ 2022-08-12 17:08  高尔赛凡尔娟  阅读(15)  评论(0编辑  收藏  举报