結果

問題 No.979 Longest Divisor Sequence
ユーザー konn13022konn13022
提出日時 2023-07-15 16:16:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 824 ms / 2,000 ms
コード長 691 bytes
コンパイル時間 2,162 ms
コンパイル使用メモリ 209,904 KB
実行使用メモリ 5,680 KB
最終ジャッジ日時 2023-10-17 02:41:03
合計ジャッジ時間 4,521 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 12 ms
4,624 KB
testcase_11 AC 11 ms
4,624 KB
testcase_12 AC 12 ms
4,624 KB
testcase_13 AC 47 ms
4,360 KB
testcase_14 AC 824 ms
5,680 KB
testcase_15 AC 277 ms
4,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];

  auto f = [](int x) {
    set<int> ans;
    for (int i = 1; i * i <= x; i++) {
      if (x % i) continue;
      ans.insert(i);
      ans.insert(x / i);
    }
    return ans;
  };

  int mx = *max_element(a.begin(), a.end());
  vector<int> dp(mx + 1);
  for (int i = 0; i < n; i++) {
    if (a[i] == 1)
      dp[a[i]] = max(dp[a[i]], 1);
    else {
      auto div = f(a[i]);
      for (int d : div) {
        if (d < a[i]) dp[a[i]] = max(dp[a[i]], dp[d] + 1);
      }
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;

  return 0;
}
0