Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
For position , the condition says the value must appear somewhere later. Same witness can be reused many times, so do not accidentally turn this into a matching problem.
Value is a very useful final sink. If and position points to it, then .
The expression just swaps adjacent parity pairs: , , etc. That explains blocks like .
If is odd, the construction works: position points to value , and every later position points to final value .
If is even and not a power of two, let . Use the same final- pattern everywhere except positions , where you splice in .
Rewrite the condition as: for each , the value must appear at some position . The witness cannot be itself because , so .
The key trick is that witnesses may be reused. If we put at the last position, then any position can point to it by setting
This swaps adjacent values:
So the pattern is not magic; it is just a bunch of positions pointing to the final .
First handle odd . Use
Formally:
This is a permutation. The map on gives all values from to except ; then supplies , and supplies .
Now verify the condition. For , use the position containing :
because is odd. For every , use :
So every odd works.
Now prove when the answer is impossible. Suppose is a power of two. If value appears at some position , then its required witness would be
because only uses lower bits. That value is outside the permutation, impossible. Therefore value must be at position .
Look at position . Its only later position is , so we would need
also outside . Contradiction. So powers of two are dead. No cute escape hatch there.
It remains to construct even non-powers of two. Let
so is the smallest set bit of . Since is even and not a power of two, .
Define the permutation like this:
Empty ranges are fine.
Why is this a permutation? The normal ranges are complete adjacent swaps, so they use exactly their own values. The special positions use the special values . Together, every value from to appears exactly once.
Now verify the condition.
For every normal position, , so it points to the final :
For , use position , where :
For , use the final position:
For , we need a later value such that . Since is the lowest set bit of , flipping bit changes to , so choose
This value appears later in the normal swapped suffix. More precisely, is even, and at position
we have
Also , because is an odd integer at least . Therefore this witness is in the suffix, and
So all even non-powers work, and only powers of two fail.
Algorithm:
The complexity is per test case for output, and extra memory if printed directly, or if stored first for cleaner code.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if ((n & (n - 1)) == 0) {
cout << -1 << '\n';
continue;
}
vector<int> p(n + 1);
if (n & 1) {
p[1] = n - 1;
for (int i = 2; i < n; i++) p[i] = i ^ 1;
p[n] = 1;
} else {
int x = n & -n;
p[1] = x + 1;
for (int i = 2; i < x; i++) p[i] = i ^ 1;
p[x] = n;
p[x + 1] = x;
for (int i = x + 2; i < n; i++) p[i] = i ^ 1;
p[n] = 1;
}
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << p[i];
}
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if ((n & (n - 1)) == 0) {
cout << -1 << '\n';
continue;
}
vector<int> p(n + 1);
if (n & 1) {
p[1] = n - 1;
for (int i = 2; i < n; i++) p[i] = i ^ 1;
p[n] = 1;
} else {
int x = n & -n;
p[1] = x + 1;
for (int i = 2; i < x; i++) p[i] = i ^ 1;
p[x] = n;
p[x + 1] = x;
for (int i = x + 2; i < n; i++) p[i] = i ^ 1;
p[n] = 1;
}
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << p[i];
}
cout << '\n';
}
return 0;
}