結果

問題 No.3204 Permuted Integer
ユーザー norioc
提出日時 2025-07-19 13:45:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 598 ms / 2,000 ms
コード長 738 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 82,044 KB
最終ジャッジ日時 2025-07-19 13:45:34
合計ジャッジ時間 11,975 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import count

LIMIT = 10**9


def f(n: int) -> list[str]:
    s = ''.join(sorted(str(n)))
    res = [s]
    while len(s) > 1 and s[0] == '0':
        s = s[1:]
        res.append(s)

    return res


d = {}
for i in count(1):
    x = i*i
    if x > LIMIT: break

    for y in f(x):
        if y not in d:
            d[y] = x


def solve():
    N = int(input())
    ans = INF
    for y in f(N):
        if y in d:
            a = ''.join(sorted(str(N)))
            b = ''.join(sorted(str(d[y])))
            if len(a) >= len(b) and a.endswith(b):
                ans = min(ans, d[y])

    if ans == INF:
        return -1
    return ans


INF = 1 << 60
T = int(input())
for _ in range(T):
    res = solve()
    print(res)
0