結果

問題 No.14 最小公倍数ソート
ユーザー titiatitia
提出日時 2021-11-02 18:27:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 976 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2024-04-20 04:25:05
合計ジャッジ時間 10,750 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
12,416 KB
testcase_01 AC 34 ms
12,416 KB
testcase_02 AC 35 ms
12,288 KB
testcase_03 AC 64 ms
12,800 KB
testcase_04 AC 847 ms
15,232 KB
testcase_05 AC 358 ms
13,824 KB
testcase_06 AC 426 ms
13,952 KB
testcase_07 AC 497 ms
14,464 KB
testcase_08 AC 647 ms
14,720 KB
testcase_09 AC 765 ms
14,976 KB
testcase_10 AC 737 ms
14,976 KB
testcase_11 AC 800 ms
14,976 KB
testcase_12 AC 811 ms
14,976 KB
testcase_13 WA -
testcase_14 AC 806 ms
15,104 KB
testcase_15 AC 814 ms
14,976 KB
testcase_16 AC 371 ms
14,336 KB
testcase_17 AC 288 ms
14,080 KB
testcase_18 AC 183 ms
13,440 KB
testcase_19 AC 556 ms
14,720 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(100,x+1)):
    if x%j==0:
        K.append(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