結果

問題 No.14 最小公倍数ソート
ユーザー vwxyzvwxyz
提出日時 2021-06-30 08:34:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,729 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 104,616 KB
最終ジャッジ日時 2023-09-08 17:47:06
合計ジャッジ時間 12,375 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
91,184 KB
testcase_01 AC 253 ms
91,280 KB
testcase_02 AC 253 ms
91,292 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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,*A=map(int,read().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):
                m=LCM(a,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