結果

問題 No.979 Longest Divisor Sequence
ユーザー noshi91noshi91
提出日時 2020-01-31 21:30:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 1,217 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 84,120 KB
実行使用メモリ 48,004 KB
最終ジャッジ日時 2023-10-17 08:46:07
合計ジャッジ時間 3,936 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
40,876 KB
testcase_01 AC 101 ms
40,876 KB
testcase_02 AC 100 ms
40,876 KB
testcase_03 AC 104 ms
40,876 KB
testcase_04 AC 111 ms
40,876 KB
testcase_05 AC 109 ms
40,876 KB
testcase_06 AC 104 ms
40,876 KB
testcase_07 AC 101 ms
40,876 KB
testcase_08 AC 104 ms
40,876 KB
testcase_09 AC 107 ms
40,876 KB
testcase_10 AC 111 ms
41,140 KB
testcase_11 AC 105 ms
41,140 KB
testcase_12 AC 110 ms
41,140 KB
testcase_13 AC 160 ms
44,256 KB
testcase_14 AC 252 ms
48,004 KB
testcase_15 AC 156 ms
44,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  constexpr int A = 300001;
  constexpr int L = 18;

  std::vector<int> primes;
  {
    std::vector<bool> sieve(A, true);
    for (int i = 2; i != A; i += 1) {
      if (!sieve[i])
        continue;
      primes.push_back(i);
      for (int j = i; j < A; j += i)
        sieve[j] = false;
    }
  }

  int n;
  std::cin >> n;
  std::vector<int> a(n);
  for (int &e : a)
    std::cin >> e;
  std::vector<std::vector<int>> dp(A, std::vector<int>(L, -1));
  for (int i = 0; i != A; i += 1)
    dp[i][0] = n;
  std::vector<std::vector<int>> idx(A, std::vector<int>());
  for (int i = 0; i != n; i += 1)
    idx[a[i]].push_back(i);
  for (int i = A; --i != 0;) {
    for (const int e : primes) {
      if (i * e >= A)
        break;
      for (int j = 0; j != L; j += 1)
        dp[i][j] = std::max(dp[i][j], dp[i * e][j]);
    }
    for (const int j : idx[i]) {
      *std::prev(std::upper_bound(dp[i].rbegin(), dp[i].rend(), j)) = j;
    }
  }
  int ans = 0;
  for (int i = 0; i != A; i += 1) {
    for (int j = 0; j != L; j += 1) {
      if (dp[i][j] != -1)
        ans = std::max(ans, j);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
0