結果

問題 No.979 Longest Divisor Sequence
コンテスト
ユーザー noshi91
提出日時 2019-10-07 22:21:04
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 1,354 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 801 ms
コンパイル使用メモリ 91,456 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2026-05-02 04:17:42
合計ジャッジ時間 3,194 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#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::vector<bool> sieve(ALim, true);
    for (usize i = 2; i != ALim; ++i) {
      if (sieve[i]) {
        prime.push_back(i);
        for (usize j = i; j < ALim; j += i) {
          sieve[j] = false;
        }
      }
    }
  }

  usize n;
  std::cin >> n;
  std::vector<std::vector<usize>> rev(ALim);
  std::vector<usize> dp(ALim, n);
  for (usize i = 0; i != n; ++i) {
    usize a;
    std::cin >> a;
    rev[a].push_back(i);
    chmin(dp[a], i);
  }
  usize len = 0;
  while (std::any_of(dp.cbegin(), dp.cend(), [n](usize x) { return x < n; })) {
    ++len;
    std::vector<usize> exc(ALim, 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]);
      }
    }
    for (usize i = 1; i != ALim; ++i) {
      usize j = 0;
      const usize size = rev[i].size();
      while (j != size && rev[i][j] <= exc[i]) {
        ++j;
      }
      if (j == size) {
        dp[i] = n;
      } else {
        dp[i] = rev[i][j];
      }
    }
  }
  std::cout << len << std::endl;
}
0