結果

問題 No.979 Longest Divisor Sequence
ユーザー rokahikou1rokahikou1
提出日時 2020-11-20 12:41:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 2,603 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 178,140 KB
実行使用メモリ 7,784 KB
最終ジャッジ日時 2023-09-30 18:22:23
合計ジャッジ時間 3,437 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,512 KB
testcase_01 AC 6 ms
6,696 KB
testcase_02 AC 6 ms
6,516 KB
testcase_03 AC 6 ms
6,568 KB
testcase_04 AC 5 ms
6,684 KB
testcase_05 AC 5 ms
6,392 KB
testcase_06 AC 6 ms
6,480 KB
testcase_07 AC 6 ms
6,664 KB
testcase_08 AC 5 ms
6,636 KB
testcase_09 AC 6 ms
6,424 KB
testcase_10 AC 9 ms
6,508 KB
testcase_11 AC 8 ms
6,420 KB
testcase_12 AC 8 ms
6,468 KB
testcase_13 AC 57 ms
7,740 KB
testcase_14 AC 254 ms
7,784 KB
testcase_15 AC 88 ms
7,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a, T val) {
    return vector<T>(a, val);
}
template <class... Ts> auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
    int to;
    T cost;
    edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

// 素数篩+素因数分解
// 初期化O(N),素因数分解O(logN)
// たくさん素因数分解するときはこっち
#include <cassert>
struct Sieve {
    int N;
    vector<ll> dat;
    Sieve(int n) : N(n + 1) {
        dat.resize(N);
        iota(All(dat), 0);
        init();
    }

    void init() {
        for(int i = 2; i * i <= N; i++) {
            if(dat[i] < i)
                continue;
            for(int j = i * i; j < N; j += i) {
                if(dat[j] == j)
                    dat[j] = i;
            }
        }
    }

    map<ll, int> factorize(ll n) {
        assert(n <= N);
        map<ll, int> ret;
        while(n > 1) {
            ret[dat[n]]++;
            n = n / dat[n];
        }
        return ret;
    }

    vector<int> divisor(ll n) {
        assert(n <= N);
        vector<int> ret;
        ret.pb(1);
        while(n > 1) {
            int k = dat[n];
            int num = 0;
            while(n % k == 0) {
                n /= k;
                num++;
            }
            int sz = ret.size();
            int tmp = 1;
            for(int i = 0; i < num; i++) {
                tmp *= k;
                for(int j = 0; j < sz; j++) {
                    ret.push_back(ret[j] * tmp);
                }
            }
        }
        sort(All(ret));
        return ret;
    }
};

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i, N) { cin >> A[i]; }
    Sieve sieve(301000);
    vector<int> dp(301000, 1);
    int res = 0;
    for(int i = N - 1; i >= 0; i--) {
        auto div = sieve.divisor(A[i]);
        for(auto d : div) {
            if(d == A[i])
                continue;
            dp[d] = max(dp[A[i]] + 1, dp[d]);
        }
        res = max(res, dp[A[i]]);
    }
    cout << res << endl;
}
0