#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;
while(t--) {
int n, x; cin >> n >> x;
if(n == 1) {
cout << x << "\n";
} else if(n == 2) {
cout << 0 << " " << x << "\n";
} else {
set<int> ans;
// ans[0] = even, ans[1] = odd;
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(it == ans.begin()) {
// don't want 1 in the array as [2 ^ 3 ^ ... ^ n - 1] = X
// erase 1 and 2
ans.erase(it);
ans.erase(2);
// add three unique elements such that
ans.insert(even_two);
ans.insert(odd_two);
ans.insert(zero);
} else {
// don't want some number except one
ans.erase(it);
ans.erase(ans.begin());
ans.insert(even);
ans.insert(odd);
ans.insert(zero);
}
} else {
ans.insert(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 is the code that passes the first subtask. What’s going wrong here?
Thanks @ssjgz. When i ran this test case, one number in my answer was overflowing the given bounds. I fixed it by xoring 1 by 2^18 and setting off the MSB of the highest number. It got accepted.
How did you get the idea of 19th bit ? How do you know that a number >= 5x10^5 will have 19 bits ? How did you calculate the bits, can anyone explain please
I used a similar approach but for cases with N%4 = 3 and N%4 = 0, I took the numbers of the form 0(this one if N % 4 = 0), x ^ i, x ^ (i + 1), x ^ i ^ (i + 1). Just brute force to find any valid i.
We have to flip 19th bit of one more number, so we will flip 19th bit of a[n-2] if after fliping its not equal to a[n-1] else we will another number which in this case is a[0].