結果

問題 No.979 Longest Divisor Sequence
ユーザー lam6er
提出日時 2025-04-15 22:35:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 700 ms / 2,000 ms
コード長 1,588 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 181,424 KB
最終ジャッジ日時 2025-04-15 22:37:02
合計ジャッジ時間 2,647 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    
    max_num = 300000
    spf = list(range(max_num + 1))
    
    for i in range(2, int(max_num**0.5) + 1):
        if spf[i] == i:
            for j in range(i*i, max_num + 1, i):
                if spf[j] == j:
                    spf[j] = i
    
    memo_factors = {}
    
    def get_factors(x):
        if x in memo_factors:
            return memo_factors[x]
        original_x = x
        factors = {}
        temp = x
        while temp > 1:
            p = spf[temp]
            while temp % p == 0:
                factors[p] = factors.get(p, 0) + 1
                temp //= p
        memo_factors[original_x] = factors
        return factors
    
    max_len = 0
    dp = [0] * (max_num + 1)
    
    for x in A:
        factors = get_factors(x)
        max_prev = 0
        divisors = [1]
        
        for p, exp in factors.items():
            new_divisors = []
            for d in divisors:
                current = d
                new_d = current
                for _ in range(exp + 1):
                    new_divisors.append(new_d)
                    if new_d < x and dp[new_d] > max_prev:
                        max_prev = dp[new_d]
                    new_d *= p
            divisors = new_divisors
        
        current_length = max_prev + 1
        if current_length > dp[x]:
            dp[x] = current_length
        if dp[x] > max_len:
            max_len = dp[x]
    
    print(max_len)

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