結果

問題 No.14 最小公倍数ソート
ユーザー titiatitia
提出日時 2021-11-02 18:33:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 941 ms / 5,000 ms
コード長 999 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-04-20 04:33:32
合計ジャッジ時間 11,889 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
12,288 KB
testcase_01 AC 37 ms
12,288 KB
testcase_02 AC 36 ms
12,288 KB
testcase_03 AC 70 ms
12,672 KB
testcase_04 AC 941 ms
15,104 KB
testcase_05 AC 408 ms
13,952 KB
testcase_06 AC 460 ms
14,080 KB
testcase_07 AC 556 ms
14,336 KB
testcase_08 AC 706 ms
14,592 KB
testcase_09 AC 851 ms
14,976 KB
testcase_10 AC 847 ms
14,976 KB
testcase_11 AC 883 ms
15,104 KB
testcase_12 AC 888 ms
15,104 KB
testcase_13 AC 906 ms
15,104 KB
testcase_14 AC 874 ms
14,976 KB
testcase_15 AC 940 ms
15,104 KB
testcase_16 AC 424 ms
14,208 KB
testcase_17 AC 330 ms
13,952 KB
testcase_18 AC 197 ms
13,440 KB
testcase_19 AC 642 ms
14,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
from collections import deque

def lcm(x,y):
    return x*y//gcd(x,y)

N=int(input())
A=list(map(int,input().split()))

ANS=[A[0]]
X=sorted(A[1:])
USE=[0]*(N-1)

FACS=[[] for i in range(10001)]
DIVS=[[] for i in range(10001)]

x=ANS[-1]
K=[]
for j in range(1,min(101,x+1)):
    if x%j==0:
        K.append(j)
        K.append(x//j)
FACS[x]=K

for x in X:
    K=[]
    for j in range(1,min(100,x+1)):
        if x%j==0:
            K.append(j)
            K.append(x//j)
    K=sorted(set(K))
    FACS[x]=K
    for k in K:
        DIVS[k].append(x)

        
for i in range(N-1):
    #print(DIVS[:10])
    a=ANS[-1]

    MIN=1<<30
    now=1000000
    
    for k in FACS[a]:
        if len(DIVS[k])==0:
            continue
        l=lcm(DIVS[k][0],a)

        if l<MIN or (l==MIN and DIVS[k][0]<now):
            now=DIVS[k][0]
            MIN=l

    ANS.append(now)

    for k in FACS[now]:
        xx=DIVS[k].index(now)
        DIVS[k].pop(xx)

print(*ANS)
            
    



0