CodeForces - 1270C Make Good [位运算]

C. Make Good
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s call an array a1,a2,…,ama1,a2,…,am of nonnegative integer numbers good if a1+a2+⋯+am=2⋅(a1⊕a2⊕⋯⊕am)a1+a2+⋯+am=2⋅(a1⊕a2⊕⋯⊕am), where ⊕⊕ denotes the bitwise XOR operation.
For example, array [1,2,3,6][1,2,3,6] is good, as 1+2+3+6=12=2⋅6=2⋅(1⊕2⊕3⊕6)1+2+3+6=12=2⋅6=2⋅(1⊕2⊕3⊕6). At the same time, array [1,2,1,3][1,2,1,3] isn’t good, as 1+2+1+3=7≠2⋅1=2⋅(1⊕2⊕1⊕3)1+2+1+3=7≠2⋅1=2⋅(1⊕2⊕1⊕3).
You are given an array of length nn: a1,a2,…,ana1,a2,…,an. Append at most 33 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 tt (1≤t≤100001≤t≤10000). The description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤105)(1≤n≤105) — the size of the array.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the elements of the array.
It is guaranteed that the sum of nn over all test cases does not exceed 105105.
Output
For each test case, output two lines.
In the first line, output a single integer ss (0≤s≤30≤s≤3) — the number of elements you want to append.
In the second line, output ss integers b1,…,bsb1,…,bs (0≤bi≤10180≤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
inputCopy
3
4
1 2 3 6
1
8
2
1 1
outputCopy
0
2
4 4
3
2 6 2

可以将所有输入合成sum和xor,加一个xor把xor消掉,这时右边为xor+sum,右边有x2的系数,所以两边再加sum+xor就平了

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
	ll n,a,b,t;
	cin>>t;
	while(t--)
    {
        ll sum=0,xo=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a,sum+=a,xo^=a;
        cout<<2<<endl<<xo<<' '<<sum+xo<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值