C - Make Good(构造,思维)

题目链接:https://codeforces.com/contest/1270/problem/C
题意:给定n个数(1<=n<=1e5)ai(1<=ai<=1e9),现在在原数组种添加最多3个数(范围为1到1e18),使得新数组满足 i = 1 m a i = 2 ( a 1 a 2 . . . a m ) \sum_{i=1}^{m}a_i=2*(a_1\bigoplus a_2\bigoplus...\bigoplus a_m)
题解:
定义原数组的累加和为 S S ,异或和为 X X ,那么当 X 2 > = S X*2>=S 时,我们可以在两边添加2个数 ( X 2 S ) / 2 (X*2-S)/2 即可;
(偷看了官方题解)为了保证最后得到的新数组的 X 1 2 > = S 1 X1*2>=S1 ,我们可以在数组加上数 2 50 2^{50} ,其正确性易证。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=200010;

int n;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		ll s=0,x=0,a;
		for(int i=1;i<=n;i++){
			scanf("%I64d",&a);
			s+=a;
			x^=a;
		}
		a=(1LL<<50)+(s%2);
		s+=a;x^=a;
		ll ans=(x*2-s)/2;
		printf("3\n%I64d %I64d %I64d\n",a,ans,ans);
	} 
	return 0;
}
发布了71 篇原创文章 · 获赞 1 · 访问量 2813

猜你喜欢

转载自blog.csdn.net/weixin_43918473/article/details/103996434