結果

問題 No.979 Longest Divisor Sequence
ユーザー noshi91noshi91
提出日時 2019-10-07 23:18:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,363 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 55,040 KB
実行使用メモリ 10,832 KB
最終ジャッジ日時 2024-04-23 01:49:29
合計ジャッジ時間 1,669 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
8,592 KB
testcase_01 AC 10 ms
8,720 KB
testcase_02 AC 14 ms
8,464 KB
testcase_03 AC 14 ms
8,464 KB
testcase_04 AC 14 ms
8,592 KB
testcase_05 AC 14 ms
8,592 KB
testcase_06 AC 18 ms
8,592 KB
testcase_07 AC 13 ms
8,588 KB
testcase_08 AC 15 ms
8,716 KB
testcase_09 AC 14 ms
8,460 KB
testcase_10 AC 18 ms
8,720 KB
testcase_11 AC 19 ms
8,592 KB
testcase_12 AC 17 ms
8,588 KB
testcase_13 AC 36 ms
10,832 KB
testcase_14 AC 120 ms
10,568 KB
testcase_15 AC 60 ms
9,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdio>
#include <vector>

int main() {
  using usize = std::size_t;
  const auto chmin = [](usize &a, const usize b) {
    if (b < a) {
      a = b;
    }
  };
  static constexpr usize ALim = 300001;
  std::vector<usize> prime;
  {
    std::array<bool, ALim> sieve = {};
    usize i = 2;
    for (; i * i <= ALim; ++i) {
      if (!sieve[i]) {
        prime.push_back(i);
        for (usize j = i * i; j < ALim; j += i) {
          sieve[j] = true;
        }
      }
    }
    for (; i < ALim; ++i) {
      if (!sieve[i]) {
        prime.push_back(i);
      }
    }
  }

  usize n;
  std::scanf("%zu", &n);
  std::vector<usize> a(n);
  std::array<usize, ALim> dp, exc;
  std::fill(dp.begin(), dp.end(), n);
  for (usize i = 0; i != n; ++i) {
    std::scanf("%zu", &a[i]);
    chmin(dp[a[i]], i);
  }
  usize len = 0;
  bool has_ln = true;
  while (has_ln) {
    ++len;
    has_ln = false;
    std::fill(exc.begin(), exc.end(), n);
    for (const usize p : prime) {
      for (usize i = 1; i * p < ALim; ++i) {
        chmin(dp[i * p], dp[i]);
        chmin(exc[i * p], dp[i]);
      }
    }
    std::fill(dp.begin(), dp.end(), n);
    for (usize i = 0; i != n; ++i) {
      if (exc[a[i]] < i) {
        chmin(dp[a[i]], i);
        has_ln = true;
      }
    }
  }
  std::printf("%zu\n", len);
}
0