結果

問題 No.2449 square_permutation
ユーザー FromBooska
提出日時 2023-09-01 09:30:45
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 815 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 82,112 KB
実行使用メモリ 125,952 KB
最終ジャッジ日時 2025-01-03 06:25:36
合計ジャッジ時間 53,097 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 4 TLE * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

# 辞書順なので前から確定していくか
# idxに対して、一番大きくて平方数になる数を探して場所をスワップ
# nの倍数だけをサーチすると4と9が交換できず失敗する
# 全探索では間に合わないだろう。。。困ったな

N = int(input())
pos = [-1]*(N+1)
for n in range(1, N+1):
    if n > N/2 and pos[n] == -1:
        pos[n] = n
        continue
    
    mx = N-N%n
    #print('n', n, 'mx', mx)
    # 倍数だけのサーチではダメだ、4が9と交換できない
    for j in range(mx, n-1, -1):
        #print('n', n, 'j', j, (n*j)**0.5, ((n*j)**0.5).is_integer())
        if pos[j] == -1 and ((n*j)**0.5).is_integer() == True:
            pos[n] = j
            pos[j] = n
            break
    #print('n', n, 'pos', pos)
print(*pos[1:])  
0