結果

問題 No.979 Longest Divisor Sequence
ユーザー noshi91noshi91
提出日時 2019-10-07 23:16:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,353 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 80,940 KB
実行使用メモリ 10,660 KB
最終ジャッジ日時 2024-04-23 01:46:52
合計ジャッジ時間 2,302 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
8,464 KB
testcase_01 AC 9 ms
8,460 KB
testcase_02 AC 14 ms
8,336 KB
testcase_03 AC 15 ms
8,460 KB
testcase_04 AC 15 ms
8,464 KB
testcase_05 AC 14 ms
8,448 KB
testcase_06 AC 19 ms
8,332 KB
testcase_07 AC 14 ms
8,340 KB
testcase_08 AC 15 ms
8,464 KB
testcase_09 AC 15 ms
8,464 KB
testcase_10 AC 19 ms
8,336 KB
testcase_11 AC 19 ms
8,340 KB
testcase_12 AC 20 ms
8,576 KB
testcase_13 AC 54 ms
10,660 KB
testcase_14 AC 182 ms
10,576 KB
testcase_15 AC 80 ms
9,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <cstddef>
#include <iostream>
#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::cin >> 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::cin >> 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::cout << len << std::endl;
}
0