結果

問題 No.1730 GCD on Blackboard in yukicoder
ユーザー ShirotsumeShirotsume
提出日時 2021-11-05 22:17:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,685 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 82,636 KB
実行使用メモリ 123,736 KB
最終ジャッジ日時 2024-04-25 17:25:50
合計ジャッジ時間 11,956 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,685 ms
122,696 KB
testcase_01 AC 80 ms
82,532 KB
testcase_02 AC 78 ms
82,688 KB
testcase_03 AC 87 ms
85,632 KB
testcase_04 AC 93 ms
85,992 KB
testcase_05 AC 97 ms
85,860 KB
testcase_06 AC 83 ms
86,052 KB
testcase_07 AC 86 ms
86,136 KB
testcase_08 AC 79 ms
84,264 KB
testcase_09 AC 88 ms
85,660 KB
testcase_10 AC 96 ms
90,364 KB
testcase_11 AC 81 ms
82,040 KB
testcase_12 AC 89 ms
82,192 KB
testcase_13 AC 430 ms
122,624 KB
testcase_14 AC 448 ms
122,588 KB
testcase_15 AC 422 ms
122,672 KB
testcase_16 AC 427 ms
122,908 KB
testcase_17 AC 421 ms
122,636 KB
testcase_18 AC 328 ms
122,580 KB
testcase_19 AC 366 ms
123,736 KB
testcase_20 AC 448 ms
119,600 KB
testcase_21 AC 457 ms
119,576 KB
testcase_22 AC 1,481 ms
122,768 KB
testcase_23 AC 154 ms
116,496 KB
testcase_24 AC 145 ms
116,508 KB
testcase_25 AC 365 ms
122,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
#N = 入力の最大
N = 1000000 + 100
sieve = [i for i in range(N + 1)]

for i in range(2,N + 1):
    if sieve[i] == i:
        for j in range(2 * i, N + 1, i):
            sieve[j] = i

def prime_fact(X):
    ret = Counter()
    if X == 1:
        return ret
    while 1:
        ret[sieve[X]] += 1
        if sieve[X] == X:
            break
        else:
            X //= sieve[X]
    return ret

def divisors(N, b):
    div = [1]
    b[1] += 1
    for p,a in prime_fact(N).items():
        m = len(div)
        for i in range(m):
            for j in range(1, a+1):
                div.append(div[i] * p**j)
                b[div[i] * p ** j] += 1
    
    return b
n = int(input())

a = list(map(int,input().split()))

#K = N - 1のとき答えはmax(a)になる
#ではK = N - 2では?2個残さないといけないので、答えは2個選んだときの最大公約数の最大値
#同様にN - K個選んだ時の最大公約数の最大値になる
#aの約数をマッピングしていく、N - K以上の最大値を探せばよい

b = [0] * (10 ** 6 + 100)
for i in range(n):
    b = divisors(a[i], b)
ans = [0] * (n + 1)
for i in range(10 ** 6 + 99):
    if b[i] != 0:
        ans[b[i]] = i
ans.reverse()
for i in range(n):
    ans[i] = max(ans[i], ans[i - 1])
    print(ans[i])
0