CF2189C2.XOR-convenience (Hard Version)

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The difference between the versions is that in this version, 1≤i≤n−11 \le i \le n-1. Note that a correct solution for the hard version is not necessarily a correct solution for the easy version.

Given a positive integer nn. Find some permutation∗^{\text{∗}} pp of length nn such that for every ii (\style{color:red}{1 \le i \le n-1}) there exists jj (\style{color:red}{i \le j \le n}) such that pi=pj⊕ip_i = p_j \oplus i †^{\text{†}}, or determine that it does not exist.

∗^{\text{∗}}A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

†^{\text{†}}⊕\oplus denotes the bitwise XOR operation.

这是该问题的困难版本。两个版本的区别在于:在本版本中,1≤i≤n−11 \le i \le n-1。注意,困难版本的一个正确解法不一定适用于简易版本。

给定一个正整数 nn。请构造一个长度为 nn 的排列∗^{\text{∗}} pp,使得对每个 ii(\style{color:red}{1 \le i \le n-1}),均存在某个 jj(\style{color:red}{i \le j \le n}),满足 pi=pj⊕ip_i = p_j \oplus i †^{\text{†}};若不存在这样的排列,则判定其不存在。

∗^{\text{∗}} 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数组中 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

†^{\text{†}} ⊕\oplus 表示按位异或运算。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The only line of each test case contains one positive integer nn (3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5) — the length of the permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例仅一行,包含一个正整数 nn(3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5)——即排列的长度。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, if there is a suitable permutation, output nn integers p1,p2,…,pnp_1,p_2,\ldots,p_n — the permutation pp. Otherwise, output −1-1.

If multiple solutions exist, output any of them.

对于每个测试用例,如果存在合适的排列,则输出 nn 个整数 p1,p2,…,pnp_1,p_2,\ldots,p_n —— 即排列 pp;否则,输出 −1-1。

如果存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    2
    3
    4

    输出#1

    2 1 3
    -1

说明/提示

In the first test case, the permutation p=[2,1,3]p = [2,1,3] fulfills the condition, as p2=1p_2 = 1 and p3⊕2=1p_3 \oplus 2 = 1.

In the second test case, there is no suitable permutation.

在第一个测试用例中,排列 p=[2,1,3]p = [2,1,3] 满足条件,因为 p2=1p_2 = 1 且 p3⊕2=1p_3 \oplus 2 = 1。

在第二个测试用例中,不存在满足条件的排列。

输入解题思路,AI测评打分。不知道怎么写?

首页