#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define IOS ios::sync_with_stdio(0); cin.tie(0);
signed main() {
IOS;
int t; cin >> t;
const int M = 32;
int even = 4e5, odd = 4e5+1, zero = 0;
int even_two = 400000, odd_two = 400002;
int inf = 5e5+1;
while(t--) {
int n, x; cin >> n >> x;
cout << n << " " << x << "\n";
if(n == 1) {
cout << x << "\n";
} else if(n == 2) {
cout << 0 << " " << x << "\n";
} else {
set<int> ans;
int till = 0;
for(int i=1;i<=n-1;i++) {
// ans.insert(i);
till = till ^ i;
}
// n - 1 elements filled till here [1, 2, 3, ... n - 1]
int last = x ^ till;
// last ^ till = x; last = x ^ till;
// if last belongs to [1, 2, 3, ... n - 1]
if((last > 0) && (last < n)) {
auto it = ans.find(last);
if(last == 1) {
// don't want 1 in the array as [2 ^ 3 ^ ... ^ n - 1] = X
// erase 1 and 2
ans.erase(it);
// till = till ^ 1;
// till = till ^ 2;
// add three unique elements such that
ans.insert(even_two);
ans.insert(odd_two);
ans.insert(zero);
// till = till ^ even_two;
// till = till ^ odd_two;
// till = till ^ zero;
} else {
// don't want some number which is greater than one
ans.erase(it);
ans.erase(ans.begin());
// till = till ^ last;
// till = till ^ 1;
ans.insert(even);
ans.insert(odd);
ans.insert(zero);
// till = till ^ even;
// till = till ^ odd;
// till = till ^ zero;
}
} else {
ans.insert(last);
till = till ^ last;
}
// int i = 0;
// cout << "SIZE: " << ans.size() << "\n";
// for(auto num : ans) {
// cout << i + 1 << " " << num << "\n";
// i += 1;
// }
// till = 0;
// for(auto num : ans) {
// till = till ^ num;
// }
cout << "XOR: " << till << "\n";
assert(ans.size() == n);
assert(till == x);
// for(auto num : ans) {
// cout << num << " ";
// }
// cout << "\n";
}
}
}
This code passes the first subtask but fails on every test case of the second subtask. I tried to debug it trying various test cases but it works on all of them. Can someone please point out why it is not working?