結果

問題 No.979 Longest Divisor Sequence
ユーザー kibuna
提出日時 2020-01-31 22:21:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,463 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 1,600 ms
コンパイル使用メモリ 174,928 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-09-17 08:45:37
合計ジャッジ時間 4,344 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

// list up all factors
template <typename T>
set<T> factors(T a) {
    set<T> facs;
    for (T i = 1; i * i <= a; ++i) {
        if (a % i == 0) {
            facs.insert(i);
            facs.insert(a / i);
        }
    }
    return facs;
}

template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint n;
    cin >> n;
    vector<lint> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<lint> dp(300001, 0);
    for (int i = 0; i < n; ++i) {
        auto facs = factors(a[i]);
        lint maxi = 0;
        for (auto &f : facs) {
            if (f == a[i])
                continue;
            chmax(maxi, dp[f]);
        }
        chmax(dp[a[i]], maxi + 1);
    }
    lint ret = 0;
    for (int i = 0; i < 300001; ++i) {
        chmax(ret, dp[i]);
    }
    cout << ret << "\n";
    return 0;
}
0