結果

問題 No.2449 square_permutation
ユーザー LyricalMaestro
提出日時 2025-05-16 02:08:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 573 ms / 2,000 ms
コード長 1,191 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 82,080 KB
実行使用メモリ 272,644 KB
最終ジャッジ日時 2025-05-16 02:08:42
合計ジャッジ時間 10,932 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/2449

from collections import deque

def main():
    N = int(input())

    # osa-k法による素因数分解
    primes = [i for i in range(N + 1)]
    for p in range(2, N + 1):
        if p != primes[p]:
            continue

        x = 2 * p
        while x <= N:
            if primes[x] > p:
                primes[x] = p
            x += p
        
    x_map = {1:[1]}
    x_array = [1]
    for p in range(2, N + 1):
        p0 = p
        primes_ = {}
        while p > 1:
            q = primes[p]
            if q not in primes_:
                primes_[q] = 0
            primes_[q] += 1
            p //= q
        
        r = 1
        for q, v in primes_.items():
            if v % 2 == 1:
                r *= q
        if r not in x_map:
            x_map[r] = []
        x_map[r].append(p0)
        x_array.append(r)

    r_map2 = {}    
    for r, r_array in x_map.items():
        r_array.sort(reverse=True)
        queue = deque(r_array)
        r_map2[r] = queue

    answer= []
    for r in x_array:
        answer.append(r_map2[r].popleft())
    
    print(" ".join(map(str, answer)))

    


if __name__ == "__main__":
    main()
0