結果

問題 No.390 最長の数列
ユーザー NoneNone
提出日時 2021-03-13 19:09:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 778 ms / 5,000 ms
コード長 3,317 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 110,232 KB
最終ジャッジ日時 2024-04-23 09:34:18
合計ジャッジ時間 5,253 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
79,400 KB
testcase_01 AC 68 ms
79,232 KB
testcase_02 AC 69 ms
80,000 KB
testcase_03 AC 70 ms
79,360 KB
testcase_04 AC 67 ms
79,616 KB
testcase_05 AC 389 ms
109,568 KB
testcase_06 AC 778 ms
109,424 KB
testcase_07 AC 63 ms
79,436 KB
testcase_08 AC 66 ms
79,488 KB
testcase_09 AC 66 ms
79,232 KB
testcase_10 AC 399 ms
109,824 KB
testcase_11 AC 401 ms
109,736 KB
testcase_12 AC 391 ms
110,232 KB
testcase_13 AC 142 ms
106,300 KB
testcase_14 AC 339 ms
110,008 KB
testcase_15 AC 63 ms
79,152 KB
testcase_16 AC 72 ms
82,432 KB
testcase_17 AC 133 ms
96,640 KB
testcase_18 AC 126 ms
96,772 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


L=10**6+10
N = int(input())
PF = PrimeFactor(L)
X=list(map(int, input().split()))
X.sort()
P=[0]*L
for i in range(N):
    x=X[i]
    if x==1:
        P[x]=1
        continue
    for y in PF.divisors(x):
        if y==x:continue
        P[x]=max(P[y]+1,P[x])


print(max(P))
0