結果

問題 No.1730 GCD on Blackboard in yukicoder
ユーザー lam6er
提出日時 2025-04-15 23:16:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 1,264 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,036 KB
実行使用メモリ 148,996 KB
最終ジャッジ日時 2025-04-15 23:18:20
合計ジャッジ時間 5,988 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    N = int(data[0])
    A = list(map(int, data[1:N+1]))
    
    max_A = max(A)
    freq = [0] * (max_A + 1)
    for a in A:
        freq[a] += 1
    
    # Compute count[d] for all d from 1 to max_A
    count = [0] * (max_A + 1)
    for d in range(1, max_A + 1):
        for multiple in range(d, max_A + 1, d):
            count[d] += freq[multiple]
    
    # Collect all d with count[d] > 0 and sort in descending order
    sorted_d = [d for d in range(1, max_A + 1) if count[d] > 0]
    sorted_d.sort(reverse=True)
    
    max_d = [0] * (N + 2)  # max_d[t] for t from 0 to N
    filled_t = 0
    
    for d in sorted_d:
        current_max_t = min(count[d], N)
        if current_max_t <= filled_t:
            continue
        start = filled_t + 1
        end = current_max_t
        if start > end:
            continue
        for t in range(start, end + 1):
            max_d[t] = d
        filled_t = end
        if filled_t >= N:
            break
    
    # Generate output for each K from 0 to N-1
    output = []
    for K in range(N):
        t = N - K
        output.append(str(max_d[t]))
    
    print('\n'.join(output))

if __name__ == "__main__":
    main()
0