Submission #720356

#TimeUsernameProblemLanguageResultExecution timeMemory
720356GrandTiger1729XOR Sum (info1cup17_xorsum)C++17
100 / 100
822 ms51752 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 30;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n; cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int ans = 0;
    if (n % 2 == 0){
        for (int j = 0; j < N; j++){
            int cnt = 0;
            for (int i = 0; i < n; i++){
                if (a[i] & 1 << j)
                    cnt++;
            }
            ans ^= (cnt & 1) << j;
        }
    }
    vector<pair<int, int>> res(n);
    for (int i = 0; i < n; i++)
        res[i] = make_pair(a[i] & 1, i);
    sort(res.begin(), res.end());
    auto calc = [&](int j) -> long long {
        int r = n;
        long long ret = 0;
        for (int i = 0; i < n; i++){
            while (r > 0 && res[r - 1].first >= (1 << j + 1) - res[i].first) r--;
            ret += n - r;
            ret += res[i].first >= (1 << j);
        }
        return ret;
    };
    long long cnt = calc(0) / 2;
    ans ^= (cnt & 1) << 1;
    for (int j = 1; j < N; j++){
        vector<pair<int, int>> b, c;
        for (int i = 0; i < n; i++){
            if (a[res[i].second] & 1 << j)
                c.push_back(make_pair(res[i].first ^ 1 << j, res[i].second));
            else
                b.push_back(res[i]);
        }
        b.insert(b.end(), c.begin(), c.end());
        swap(res, b);
        cnt = calc(j) / 2;
        ans ^= (cnt & 1) << j + 1;
    }
    cout << ans << '\n';
    return 0;
}

Compilation message (stderr)

xorsum.cpp: In lambda function:
xorsum.cpp:30:57: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   30 |             while (r > 0 && res[r - 1].first >= (1 << j + 1) - res[i].first) r--;
      |                                                       ~~^~~
xorsum.cpp: In function 'int main()':
xorsum.cpp:49:31: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   49 |         ans ^= (cnt & 1) << j + 1;
      |                             ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...