No.3204 Permuted Integer
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
Problem Statement
You are given a positive integer $N$. Determine whether any of the integers obtained by rearranging the digits of the decimal representation of $N$ is a perfect square.
If such integers exist, output the smallest such perfect square; otherwise, output -1.
The result of the rearrangement may have unnecessary leading $0$s (leading zeros), but the output must not contain unnecessary $0$s.
Constraints
- $1 \leq T \leq 10^5$
- $1 \leq N \leq 10^9$
- All input values are integers.
Input
$T$ $N_1$ $N_2$ $\vdots$ $N_T$
Output
Output $T$ lines. On the $i \ (1 \leq i \leq T)$-th line, output the answer for the $i$-th test case.
Sample
Sample 1
Input
6 18 2025 2024 8886519 62506357 4000
Output
81 225 -1 9865881 6375625 4
For the $1$st test case, 18 becomes a perfect square when rearranged into 81.
For the $2$nd test case, the rearrangements of 2025 that are perfect squares are 0225 and 2025. The minimum is 0225 = 225, so output 225.
For the $3$rd test case, no rearrangement of 2024 is a perfect square, so output -1.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。