結果

問題 No.979 Longest Divisor Sequence
ユーザー kibunakibuna
提出日時 2020-01-31 22:21:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,462 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 174,996 KB
実行使用メモリ 7,984 KB
最終ジャッジ日時 2023-10-17 10:20:35
合計ジャッジ時間 4,565 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,608 KB
testcase_01 AC 4 ms
5,608 KB
testcase_02 AC 4 ms
5,608 KB
testcase_03 AC 4 ms
5,608 KB
testcase_04 AC 4 ms
5,608 KB
testcase_05 AC 4 ms
5,608 KB
testcase_06 AC 4 ms
5,608 KB
testcase_07 AC 4 ms
5,608 KB
testcase_08 AC 4 ms
5,608 KB
testcase_09 AC 4 ms
5,608 KB
testcase_10 AC 19 ms
5,608 KB
testcase_11 AC 19 ms
5,608 KB
testcase_12 AC 19 ms
5,608 KB
testcase_13 AC 37 ms
7,984 KB
testcase_14 AC 1,462 ms
7,984 KB
testcase_15 AC 488 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

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