답안 #681913

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681913 2023-01-14T20:57:16 Z MilosMilutinovic XOR Sum (info1cup17_xorsum) C++14
100 / 100
1433 ms 34432 KB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;

typedef long long ll;

const int N = 1000001;
const int LOG = 30;
int n;
int v[N];
vector<int> xs[2];
vector<int> ys[2];
int pos[N];
int id[N];

ll calcSame(vector<int> x, vector<int> y, int b) {
	assert(x == y);
	ll r = 0;
	int ptr = (int)y.size();
	for (int i = 0; i < (int) x.size(); i++) {
		while(ptr > 0 && x[i] + y[ptr - 1] >= (1 << b)) ptr--;
		r += (int)y.size() - ptr;
	}
	for (int p : x)
		if (p + p >= (1 << b))
			r++;
	return r / 2;
}
ll calcDiff(vector<int> x, vector<int> y, int b) {
	ll r = 0;
	int ptr = (int)y.size();
	for (int i = 0; i < (int) x.size(); i++) {
		while(ptr > 0 && x[i] + y[ptr - 1] >= (1 << b)) ptr--;
		r += (int)y.size() - ptr;
	}
	return (ll)x.size() * y.size() - r;
}

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &v[i]);
	}
	int ans = 0;
	ll a = 0, b = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] & 1)
			a++;
		else
			b++;
	}
	int T = 0;
	for (int j = 0; j < 2; j++) {
		for (int i = 0; i < n; i++) {
			if (v[i] % 2 == j)
				pos[i] = T++;
		}
	}
	if (a * b % 2 != 0)
		ans ^= 1;
	for (int b = 1; b < LOG; b++) {
		for (int i = 0; i < n; i++)
			id[pos[i]] = i;
		for (int j = 0; j < 2; j++)
			xs[j].clear();
		for (int i = 0; i < n; i++) {
			int bit = (v[id[i]] >> b & 1);
			xs[bit].push_back(id[i]);
		}
		int t = 0;
		for (int j = 0; j < 2; j++)
			for (int i : xs[j])
				pos[i] = t++;
		for (int j = 0; j < 2; j++)
			ys[j].clear();
		for (int j = 0; j < 2; j++) {
			for (int i : xs[j]) {
				ys[j].push_back(v[i] & ((1 << b) - 1));
			}
		}
		ll c = 0;
		for (int x = 0; x < 2; x++) {
			for (int y = x; y < 2; y++) {
				if (x == y)
					c += calcSame(ys[x], ys[y], b);
				else
					c += calcDiff(ys[x], ys[y], b);
			}
		}
		if (c & 1) {
			ans += (1 << b);
		}
	}
	printf("%d\n", ans);

	return 0;
}

Compilation message

xorsum.cpp: In function 'int main()':
xorsum.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
xorsum.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d", &v[i]);
      |   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 4 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1129 ms 34076 KB Output is correct
2 Correct 1008 ms 31884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1129 ms 34076 KB Output is correct
2 Correct 1008 ms 31884 KB Output is correct
3 Correct 1148 ms 34076 KB Output is correct
4 Correct 1107 ms 32968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 4 ms 456 KB Output is correct
3 Correct 104 ms 3532 KB Output is correct
4 Correct 103 ms 3540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 4 ms 456 KB Output is correct
3 Correct 1129 ms 34076 KB Output is correct
4 Correct 1008 ms 31884 KB Output is correct
5 Correct 1148 ms 34076 KB Output is correct
6 Correct 1107 ms 32968 KB Output is correct
7 Correct 104 ms 3532 KB Output is correct
8 Correct 103 ms 3540 KB Output is correct
9 Correct 1303 ms 34308 KB Output is correct
10 Correct 1250 ms 34280 KB Output is correct
11 Correct 1433 ms 34432 KB Output is correct