CF1713C.Build Permutation

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A 0\mathbf{0}-indexed array aa of size nn is called good if for all valid indices ii (0≤i≤n−10 \le i \le n-1), ai+ia_i + i is a perfect square†^\dagger.

Given an integer nn. Find a permutation‡^\ddagger pp of [0,1,2,…,n−1][0,1,2,\ldots,n-1] that is good or determine that no such permutation exists.

†^\dagger An integer xx is said to be a perfect square if there exists an integer yy such that x=y2x = y^2.

‡^\ddagger An array bb is a permutation of an array aa if bb consists of the elements of aa in arbitrary order. For example, [4,2,3,4][4,2,3,4] is a permutation of [3,2,4,4][3,2,4,4] while [1,2,2][1,2,2] is not a permutation of [1,2,3][1,2,3].

一个大小为 nn 的、以 0\mathbf{0} 为起始索引的数组 aa 被称为好数组,当且仅当对所有合法下标 ii(即 0≤i≤n−10 \le i \le n-1),ai+ia_i + i 均为完全平方数†^\dagger。

给定一个整数 nn,请找出 [0,1,2,…,n−1][0,1,2,\ldots,n-1] 的一个排列‡^\ddagger pp,使得 pp 是好数组;若不存在这样的排列,则判定其不存在。

†^\dagger 整数 xx 被称为完全平方数,当且仅当存在整数 yy,使得 x=y2x = y^2。

‡^\ddagger 数组 bb 是数组 aa 的一个排列,当且仅当 bb 由 aa 的所有元素以任意顺序组成。例如,[4,2,3,4][4,2,3,4] 是 [3,2,4,4][3,2,4,4] 的一个排列,而 [1,2,2][1,2,2] 不是 [1,2,3][1,2,3] 的排列。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The only line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例仅有一行,包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 排列 pp 的长度。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, output nn distinct integers p0,p1,…,pn−1p_0, p_1, \dots, p_{n-1} (0≤pi≤n−10 \le p_i \le n-1) — the permutation pp — if the answer exists, and −1-1 otherwise.

对于每个测试用例,如果答案存在,则输出 nn 个互不相同的整数 p0,p1,…,pn−1p_0, p_1, \dots, p_{n-1}(满足 0≤pi≤n−10 \le p_i \le n-1),即排列 pp;否则输出 −1-1。

输入输出样例

  • 输入#1

    3
    3
    4
    7

    输出#1

    1 0 2 
    0 3 2 1 
    1 0 2 6 5 4 3

说明/提示

In the first test case, we have n=3n=3. The array p=[1,0,2]p = [1, 0, 2] is good since 1+0=121 + 0 = 1^2, 0+1=120 + 1 = 1^2, and 2+2=222 + 2 = 2^2

In the second test case, we have n=4n=4. The array p=[0,3,2,1]p = [0, 3, 2, 1] is good since 0+0=020 + 0 = 0^2, 3+1=223 + 1 = 2^2, 2+2=222+2 = 2^2, and 1+3=221+3 = 2^2.

在第一个测试用例中,我们有 n=3n=3。数组 p=[1,0,2]p = [1, 0, 2] 是“好”的,因为 1+0=121 + 0 = 1^2、0+1=120 + 1 = 1^2 且 2+2=222 + 2 = 2^2。

在第二个测试用例中,我们有 n=4n=4。数组 p=[0,3,2,1]p = [0, 3, 2, 1] 是“好”的,因为 0+0=020 + 0 = 0^2、3+1=223 + 1 = 2^2、2+2=222+2 = 2^2 且 1+3=221+3 = 2^2。

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

首页