其他分享
首页 > 其他分享> > CF Round#626 D - Present

CF Round#626 D - Present

作者:互联网

D - Present

位运算 + 思维 + 二分(双指针)

  1. 按位考虑,第 \(k\) 位是 0 还是 1 只跟前 \(k\) 位有关,因此算第 \(k\) 位的答案时可对 \(a\) 数组的元素 \(\mod 2^{k+1}\) 赋给 \(b\)
  2. 若 \(b_i+b_j\) 第 \(k\) 位是 \(1\), 则 \(a_i+a_j\) 的值域为 \([2^k,2^k-1],\;[2^k+2^{k+1},2^{k+2}-2]\)
  3. 对 \(b\) 数组从小到大排序,可枚举 \(i\), 二分找到满足 \(b_i+b_j\) 在上述值域内的 \(j\) 的范围,共有奇数个则这一位是 \(1\)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 4e5 + 10;
int a[N], b[N];
int n;
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	int ans = 0;
	for (int k = 0; k < 27; k++)
	{
		for (int i = 1; i <= n; i++)
			b[i] = a[i] % (1 << (k + 1));
		sort(b + 1, b + n + 1);
		for (int i = 1; i <= n; i++)
		{
			int cnt = 0;
			int L = max(0, (1 << k) - b[i]);
			int R = max(0, (1 << k + 1) - 1 - b[i]);
			cnt += upper_bound(b + i + 1, b + n + 1, R) - lower_bound(b + i + 1, b + n + 1, L);
			
			L = max(0, (1 << k + 1) + (1 << k) - b[i]);
			R = max(0, (1 << k + 2) - 2 - b[i]);
			cnt += upper_bound(b + i + 1, b + n + 1, R) - lower_bound(b + i + 1, b + n + 1, L);
			if (cnt & 1)
				ans ^= 1 << k;
		}
	}
	cout << ans << endl;
	return 0;
}

标签:626,int,位是,CF,long,tie,include,Present
来源: https://www.cnblogs.com/hzy717zsy/p/16289770.html