結果

問題 No.979 Longest Divisor Sequence
ユーザー NoneNone
提出日時 2021-02-27 20:51:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,049 ms / 2,000 ms
コード長 3,314 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 121,232 KB
最終ジャッジ日時 2024-04-10 15:50:26
合計ジャッジ時間 3,145 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,076 KB
testcase_01 AC 37 ms
53,348 KB
testcase_02 AC 37 ms
53,568 KB
testcase_03 AC 38 ms
53,540 KB
testcase_04 AC 38 ms
53,604 KB
testcase_05 AC 37 ms
53,564 KB
testcase_06 AC 36 ms
53,404 KB
testcase_07 AC 36 ms
53,544 KB
testcase_08 AC 36 ms
53,536 KB
testcase_09 AC 37 ms
53,444 KB
testcase_10 AC 101 ms
83,548 KB
testcase_11 AC 94 ms
82,560 KB
testcase_12 AC 102 ms
83,304 KB
testcase_13 AC 119 ms
121,232 KB
testcase_14 AC 1,049 ms
118,056 KB
testcase_15 AC 397 ms
96,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class PrimeFactor():

    def __init__(self, n):
        """
        エラトステネス O(N loglog N)
        """
        self.n = n
        self.table = list(range(n+1))       # 最小素因数のリスト
        self.table[2::2] = [2]*(n//2)
        for p in range(3, int(n**0.5) + 2, 2):
            if self.table[p] == p:
                for q in range(p * p, n + 1, 2 * p):
                    if self.table[q] == q:
                        self.table[q] = p

    def is_prime(self, x):
        """ 素数判定 O(1) """
        if x < 2:
            return False
        return self.table[x] == x

    def prime_factors(self, x):
        """ 素因数分解 O(logN)  (試し割りだとO(sqrt(N))) """
        res = []
        if x < 2:
            return res
        while self.table[x] != 1:
            res.append(self.table[x])
            x //= self.table[x]
        return res

    def divisors(self, x):
        """ 約数列挙 x=[1,10**6]の約数全列挙も間に合う """
        primes=self.prime_counter(x)
        P=set([1])
        for key, value in primes.items():
            Q=[]
            for p in P:
                for k in range(value+1):
                    Q.append(p*pow(key,k))
            P|=set(Q)
        P = list(P)
        P.sort()
        return P

    def prime_counter(self, x):
        """
        素因数分解(個数のリスト) O(logN)
        {素因数: 個数} の形で返す
        """
        res = dict()
        if x < 2:
            return res
        while self.table[x] != 1:
            res[self.table[x]] = res.get(self.table[x], 0) + 1
            x //= self.table[x]
        return res

    def divisors_counter(self, x):
        """ 約数の個数 O((logN)^2) """
        res = 1
        for value in self.prime_counter(x).values():
            res *= (value+1)
        return res

    def prime_gcd(self, X, mod=None):
        """ n個の最大公約数 X:n個のリスト  (O((logN)^2)) """
        exponents = self.prime_counter(X[0])
        for x in X[1:]:
            Y = self.prime_counter(x)
            for prime, exp in exponents.items():
                if Y[prime] < exp:
                    exponents[prime] = Y[prime]
        res = 1
        for prime, exp in exponents.items():
            res *= pow(prime, exp, mod)
        if mod == None:
            return res
        else:
            return res % mod

    def prime_lcm(self, X, mod=None):
        """ n個の最小公倍数 X:n個のリスト  (O((logN)^2)) """
        exponents = dict()
        for x in X:
            for prime, exp in self.prime_counter(x).items():
                if exp > exponents.get(prime, 0):
                    exponents[prime] = exp
        res = 1
        for prime, exp in exponents.items():
            res *= pow(prime, exp, mod)
        if mod == None:
            return res
        else:
            return res % mod


#####################################################################################################
import sys
input = sys.stdin.readline


N=int(input())
A=list(map(int, input().split()))
PF = PrimeFactor(max(A)+1)
dp=[-1]*(max(A) + 1)
dp[1]=0
for a in A:
    tmp=0
    for n in PF.divisors(a):
        if n==a: continue
        if dp[n]==-1: continue
        tmp=max(tmp,dp[n])
    dp[a]=tmp+1
print(max(dp))
0