CodeForces 1323D Present

19 篇文章 0 订阅

一、内容

Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute(a1+a2)⊕(a1+a3)⊕…⊕(a1+an)⊕(a2+a3)⊕…⊕(a2+an)…⊕(an−1+an)Here x⊕yis a bitwise XOR operation (i.e. x ^ yin many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.

Input

The first line contains a single integer n(2≤n≤400000) — the number of integers in the array.The second line contains integers a1,a2,…,an(1≤ai≤107 ).

Output

Print a single integer — xor of all pairwise sums of integers in the given array.

Examples

Input

2
1 2

Output

3

Input

3
1 2 3

Output

2

加粗样式

二、思路

在这里插入图片描述

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm> 
using namespace std;
const int N = 4e5 + 5;
int n, a[N], b[N], ans;
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 0; i <= 25; i++) { //枚举答案的每一位 
		int mod = 1 << (i + 1);
		for (int j = 1; j <= n; j++) b[j] = a[j] % mod; //去掉i前面的位
		sort(b + 1, b + 1 + n);
		int cnt = 0;
		for (int k = 1; k <= n; k++) {//固定a[k] 
			//[2^i - b[k], 2^i+1 - 1 - b[k]]
			int l = lower_bound(b + 1, b + 1 + n, (1 << i) - b[k]) - b ;			
			int r = lower_bound(b + 1, b + 1 + n, (1 << (i+1)) - b[k]) - b - 1;
			cnt += r - l + 1;
			//[2^i+1 + 2 ^ i- b[k], 2^i+2 - 1 - b[k]]
			l = lower_bound(b + 1, b + 1 + n, (1 << (i+1)) + (1 << i) - b[k]) - b;			
			cnt += n - l + 1;				
			if ((b[k] + b[k]) & (1 << i)) cnt--; //自己和自己要去掉		
		} 
		// /2 是因为我统计了2倍的个数  比如b1 + b2 在 b2 + b1的时候重复了 
		if ((cnt / 2) & 1) ans += 1 << i;
	}
	printf("%d\n", ans);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值