結果

問題 No.14 最小公倍数ソート
ユーザー vwxyzvwxyz
提出日時 2021-06-30 08:52:00
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,768 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 87,060 KB
実行使用メモリ 97,704 KB
最終ジャッジ日時 2023-09-08 18:01:42
合計ジャッジ時間 9,026 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 259 ms
91,356 KB
testcase_01 AC 264 ms
91,256 KB
testcase_02 AC 261 ms
91,436 KB
testcase_03 AC 621 ms
97,704 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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 dct.keys():
        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