CF1713C.Build Permutation
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A 0-indexed array a of size n is called good if for all valid indices i (0≤i≤n−1), ai+i is a perfect square†.
Given an integer n. Find a permutation‡ p of [0,1,2,…,n−1] that is good or determine that no such permutation exists.
† An integer x is said to be a perfect square if there exists an integer y such that x=y2.
‡ An array b is a permutation of an array a if b consists of the elements of a in arbitrary order. For example, [4,2,3,4] is a permutation of [3,2,4,4] while [1,2,2] is not a permutation of [1,2,3].
一个大小为 n 的、以 0 为起始索引的数组 a 被称为好数组,当且仅当对所有合法下标 i(即 0≤i≤n−1),ai+i 均为完全平方数†。
给定一个整数 n,请找出 [0,1,2,…,n−1] 的一个排列‡ p,使得 p 是好数组;若不存在这样的排列,则判定其不存在。
† 整数 x 被称为完全平方数,当且仅当存在整数 y,使得 x=y2。
‡ 数组 b 是数组 a 的一个排列,当且仅当 b 由 a 的所有元素以任意顺序组成。例如,[4,2,3,4] 是 [3,2,4,4] 的一个排列,而 [1,2,2] 不是 [1,2,3] 的排列。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The only line of each test case contains a single integer n (1≤n≤105) — the length of the permutation p.
It is guaranteed that the sum of n over all test cases does not exceed 105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例仅有一行,包含一个整数 n(1≤n≤105)—— 排列 p 的长度。
保证所有测试用例的 n 之和不超过 105。
输出格式
For each test case, output n distinct integers p0,p1,…,pn−1 (0≤pi≤n−1) — the permutation p — if the answer exists, and −1 otherwise.
对于每个测试用例,如果答案存在,则输出 n 个互不相同的整数 p0,p1,…,pn−1(满足 0≤pi≤n−1),即排列 p;否则输出 −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=3. The array p=[1,0,2] is good since 1+0=12, 0+1=12, and 2+2=22
In the second test case, we have n=4. The array p=[0,3,2,1] is good since 0+0=02, 3+1=22, 2+2=22, and 1+3=22.
在第一个测试用例中,我们有 n=3。数组 p=[1,0,2] 是“好”的,因为 1+0=12、0+1=12 且 2+2=22。
在第二个测试用例中,我们有 n=4。数组 p=[0,3,2,1] 是“好”的,因为 0+0=02、3+1=22、2+2=22 且 1+3=22。
输入解题思路,AI测评打分。不知道怎么写?