Table of Contents

ExplanationImplementation

Official Editorial (C++)

Explanation

To solve this problem, notice that the result of performing XOR operations on a sequence of ai<500a_i < 500 is strictly less than 292^9. That's because the ninth bit is the largest possible in the binary representation of aia_i.

With this observation, we can now formulate our DP solution. Let dp[x][i]dp[x][i] represent the feasibility of getting xx as a result of XOR of an increasing sequence ending with a number less than or equal to ii. There exists the following recurrence:

dp[xai][i]=dp[xai][0..i]dp[x][0..i1]dp[x \oplus a_i][i] = dp[x \oplus a_i][0..i] \lor dp[x][0..i-1]

with

dp[0][i]=truedp[0][i] = \text{true}

since it is always possible to get 0 from an increasing sequence consisting of zero element.

Implementation

Time Complexity: O(n2log2(MAXA))\mathcal{O}(n \cdot 2^{\lceil \log_2(\text{MAXA}) \rceil})

C++

#include <bits/stdc++.h>
using namespace std;
const int MAXA = 1 << 9;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int &i : arr) { cin >> i; }

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!