#include "machine.h"
#include <vector>
#include <numeric>
std::vector<int> find_permutation(int N) {
// 1. Prepare input array A where A[j] = j.
// This satisfies M <= N+2 constraint for Subtasks 4-6.
std::vector<int> A(N);
for (int j = 0; j < N; ++j) {
A[j] = j;
}
// 2. Make the single allowed call to the machine.
std::vector<int> B = use_machine(A);
// 3. Find the hidden constant X.
// We check all possible values of X (0 to 255).
int X = -1;
for (int candidate_x = 0; candidate_x < 256; ++candidate_x) {
std::vector<bool> seen(N, false);
bool is_valid = true;
for (int i = 0; i < N; ++i) {
int original_val = B[i] ^ candidate_x;
// Check if this X makes every P[i] a unique value within [0, N-1]
if (original_val < 0 || original_val >= N || seen[original_val]) {
is_valid = false;
break;
}
seen[original_val] = true;
}
if (is_valid) {
X = candidate_x;
break;
}
}
// 4. Reconstruct the permutation P.
// Since B[i] = A[P[i]] ^ X and A[j] = j, then B[i] = P[i] ^ X.
std::vector<int> P(N);
for (int i = 0; i < N; ++i) {
P[i] = B[i] ^ X;
}
return P;
}