---
id: 2436
lang: en
translation_of: index.md
source_sha256: e8449f5be0f9eb0b23a3f4b66d9c03126afcf7b0b1a0c594135d68dfd966149b
title: "[LibreOJ] #6060 [2017 Shandong Round 1 Training Camp Day 1 / SDWC2018 Day1] Set - Solution"
description: "Give as many bits of the total XOR as possible to x2, building the linear basis with the higher zero bits of the total prioritized."
tags: ["OI","Greedy","Linear Basis"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

A <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span> problem.

Notice that $x_1 \oplus x_2 = xorsum$, so we try to give as many bits of $xorsum$ as possible to $x_2$. If some bit of $xorsum$ is $1$, just give it straight to $x_2$; if it is $0$, there are two cases, $0, 0$ and $1, 1$. So when we build the linear basis, prioritizing the higher bits that are $0$ makes the answer larger (think about the process of finding the maximum XOR sum with a linear basis: positions with greater weight are inserted first).

<!--more-->

## Code

```cpp
// LOJ6060.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;

typedef long long ll;

int n, tot;
ll ai[MAX_N], tb[MAX_N], bi[MAX_N];

void insert(ll x)
{
    for (int i = 1; i <= tot; i++)
        if (x & (1LL << tb[i]))
        {
            if (bi[i] == 0)
            {
                bi[i] = x;
                break;
            }
            x ^= bi[i];
        }
}

int main()
{
    scanf("%d", &n);
    ll xorsum = 0;
    for (int i = 1; i <= n; i++)
        scanf("%lld", &ai[i]), xorsum ^= ai[i];
    for (int i = 62; i >= 0; i--)
        if (!(xorsum & (1LL << i)))
            tb[++tot] = i;
    for (int i = 62; i >= 0; i--)
        if (xorsum & (1LL << i))
            tb[++tot] = i;
    for (int i = 1; i <= n; i++)
        insert(ai[i]);
    ll x2 = 0;
    for (int i = 1; i <= tot; i++)
        if (!(x2 & (1LL << tb[i])))
            x2 ^= bi[i];
    printf("%lld\n", xorsum ^ x2);
    return 0;
}
```
