結果

問題 No.14 最小公倍数ソート
ユーザー vwxyzvwxyz
提出日時 2021-06-30 09:02:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 621 ms / 5,000 ms
コード長 1,768 bytes
コンパイル時間 416 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 104,752 KB
最終ジャッジ日時 2023-09-08 18:12:14
合計ジャッジ時間 12,081 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 249 ms
91,276 KB
testcase_01 AC 249 ms
91,052 KB
testcase_02 AC 249 ms
91,280 KB
testcase_03 AC 390 ms
98,404 KB
testcase_04 AC 601 ms
104,716 KB
testcase_05 AC 523 ms
101,080 KB
testcase_06 AC 520 ms
100,524 KB
testcase_07 AC 566 ms
102,400 KB
testcase_08 AC 582 ms
102,868 KB
testcase_09 AC 586 ms
102,668 KB
testcase_10 AC 600 ms
103,120 KB
testcase_11 AC 601 ms
104,160 KB
testcase_12 AC 593 ms
103,672 KB
testcase_13 AC 592 ms
103,904 KB
testcase_14 AC 613 ms
104,040 KB
testcase_15 AC 621 ms
104,752 KB
testcase_16 AC 541 ms
101,152 KB
testcase_17 AC 525 ms
101,564 KB
testcase_18 AC 486 ms
99,624 KB
testcase_19 AC 577 ms
102,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter, deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines

def Divisors(N):
    divisors=[]
    for i in range(1,N+1):
        if i**2>=N:
            break
        elif N%i==0:
            divisors.append(i)
    if i**2==N:
        divisors+=[i]+[N//i for i in divisors[::-1]]
    else:
        divisors+=[N//i for i in divisors[::-1]]
    return divisors

def LCM(n,m):
    if n or m:
        return abs(n)*abs(m)//math.gcd(n,m)
    return 0

N=int(readline())
A=list(map(int,readline().split()))
dct=defaultdict(list)
for i,a in enumerate(A):
    for d in Divisors(a):
        dct[d].append((a,i))
for d in dct.keys():
    heapify(dct[d])
used=[False]*N
ans_lst=[0]
used[0]=True
a=A[0]
for _ in range(N-1):
    m=(float('inf'),)
    m_i=None
    for d in Divisors(a):
        if not dct[d]:
            continue
        aa,i=dct[d][0]
        while used[i]:
            heappop(dct[d])
            if not dct[d]:
                break
            aa,i=dct[d][0]
        else:
            if m>(LCM(a,aa),aa):
                m=(LCM(a,aa),aa)
                m_i=i
    ans_lst.append(m_i)
    used[m_i]=True
    a=A[m_i]
ans_lst=[A[i] for i in ans_lst]
print(*ans_lst)
0