結果

問題 No.2218 Multiple LIS
ユーザー stoq
提出日時 2022-06-18 00:31:04
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 79 ms / 3,000 ms
コード長 1,639 bytes
コンパイル時間 2,628 ms
コンパイル使用メモリ 209,016 KB
最終ジャッジ日時 2025-01-29 22:55:21
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_N = 1e5 + 10;
int min_factor[MAX_N] = {};

struct init_prime {
  init_prime() {
    min_factor[1] = -1;
    for (int i = 2; i < MAX_N; i++) {
      if (min_factor[i] != 0) continue;
      for (int j = i + i; j < MAX_N; j += i) min_factor[j] = i;
    }
  }
} init_prime;

void factorization(int n, unordered_map<int, int> &res) {
  if (n <= 1) return;
  if (!min_factor[n]) {
    ++res[n];
    return;
  }
  ++res[min_factor[n]];
  factorization(n / min_factor[n], res);
}

void dfs(unordered_map<int, int>::iterator itr, int prod,
         unordered_map<int, int> &mp, vector<int> &res) {
  if (itr == mp.end()) {
    res.emplace_back(prod);
    return;
  }
  auto [p, e] = *itr;
  for (int i = 0; i <= e; i++) {
    dfs(next(itr), prod, mp, res);
    prod *= p;
  }
}
vector<int> divisors(int n) {
  unordered_map<int, int> mp;
  factorization(n, mp);
  vector<int> res;
  dfs(mp.begin(), 1, mp, res);
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &&t : a) cin >> t;
  vector<int> dp(100010, 0);
  for (int i = 0; i < n; i++) {
    int Max = 0;
    vector<int> ds = divisors(a[i]);
    for (auto d : ds) chmax(Max, dp[d]);
    dp[a[i]] = Max + 1;
  }
  cout << *max_element(begin(dp), end(dp)) << "\n";
}
0