Submission #720356

# Submission time Handle Problem Language Result Execution time Memory
720356 2023-04-08T04:58:13 Z GrandTiger1729 XOR Sum (info1cup17_xorsum) C++17
100 / 100
822 ms 51752 KB
#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

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 time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 551 ms 41776 KB Output is correct
2 Correct 501 ms 41468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 551 ms 41776 KB Output is correct
2 Correct 501 ms 41468 KB Output is correct
3 Correct 654 ms 48540 KB Output is correct
4 Correct 650 ms 47072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 4 ms 468 KB Output is correct
3 Correct 81 ms 5060 KB Output is correct
4 Correct 75 ms 5032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 4 ms 468 KB Output is correct
3 Correct 551 ms 41776 KB Output is correct
4 Correct 501 ms 41468 KB Output is correct
5 Correct 654 ms 48540 KB Output is correct
6 Correct 650 ms 47072 KB Output is correct
7 Correct 81 ms 5060 KB Output is correct
8 Correct 75 ms 5032 KB Output is correct
9 Correct 816 ms 51752 KB Output is correct
10 Correct 822 ms 50896 KB Output is correct
11 Correct 809 ms 51020 KB Output is correct