結果

問題 No.979 Longest Divisor Sequence
ユーザー 👑 hitonanodehitonanode
提出日時 2020-01-31 22:12:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 2,625 bytes
コンパイル時間 1,759 ms
コンパイル使用メモリ 183,020 KB
実行使用メモリ 5,868 KB
最終ジャッジ日時 2023-10-17 10:03:33
合計ジャッジ時間 2,822 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,868 KB
testcase_01 AC 5 ms
5,868 KB
testcase_02 AC 6 ms
5,868 KB
testcase_03 AC 6 ms
5,868 KB
testcase_04 AC 5 ms
5,868 KB
testcase_05 AC 5 ms
5,868 KB
testcase_06 AC 5 ms
5,868 KB
testcase_07 AC 6 ms
5,868 KB
testcase_08 AC 5 ms
5,868 KB
testcase_09 AC 6 ms
5,868 KB
testcase_10 AC 8 ms
5,868 KB
testcase_11 AC 8 ms
5,868 KB
testcase_12 AC 8 ms
5,868 KB
testcase_13 AC 60 ms
5,868 KB
testcase_14 AC 214 ms
5,868 KB
testcase_15 AC 75 ms
5,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }

// Sieve of Eratosthenes
// (*this)[i] = (divisor of i, greater than 1)
// Example: [0, 1, 2, 3, 2, 5, 3, 7, 2, 3, 2, 11, ...]
// Complexity: Space O(MAXN), Time (construction) O(MAXNloglogMAXN)
struct SieveOfEratosthenes : std::vector<int>
{
    std::vector<int> primes;
    SieveOfEratosthenes(int MAXN) : std::vector<int>(MAXN + 1) {
        std::iota(begin(), end(), 0);
        for (int i = 2; i <= MAXN; i++) {
            if ((*this)[i] == i) {
                primes.push_back(i);
                for (int j = i; j <= MAXN; j += i) (*this)[j] = i;
            }
        }
    }
    using T = long long int;
    // Prime factorization for x <= MAXN^2
    // Complexity: O(log x)          (x <= MAXN)
    //             O(MAXN / logMAXN) (MAXN < x <= MAXN^2)
    std::map<T, int> Factorize(T x) {
        assert(x <= 1LL * (int(size()) - 1) * (int(size()) - 1));
        std::map<T, int> ret;
        if (x < int(size())) {
            while (x > 1) {
                ret[(*this)[x]]++;
                x /= (*this)[x];
            }
        }
        else {
            for (auto p : primes) {
                while (!(x % p)) x /= p, ret[p]++;
                if (x == 1) break;
            }
            if (x > 1) ret[x]++;
        }
        return ret;
    }
    std::vector<T> Divisors(int x) {
        std::vector<T> ret{1};
        for (auto p : Factorize(x)) {
            int n = ret.size();
            for (int i = 0; i < n; i++) {
                for (T a = 1, d = 1; d <= p.second; d++) {
                    a *= p.first;
                    ret.push_back(ret[i] * a);
                }
            }
        }
        return ret; // Not sorted
    }
    // Moebius function Table
    // return: [0=>0, 1=>1, 2=>-1, 3=>-1, 4=>0, 5=>-1, 6=>1, 7=>-1, 8=>0, ...]
    std::vector<int> GenerateMoebiusFunctionTable() {
        std::vector<int> ret(size());
        for (int i = 1; i < int(size()); i++) {
            if (i == 1) ret[i] = 1;
            else if ((i / (*this)[i]) % (*this)[i] == 0) ret[i] = 0;
            else ret[i] = -ret[i / (*this)[i]];
        }
        return ret;
    }
};
constexpr int MAXI = 300000;
SieveOfEratosthenes sieve(MAXI);

int main()
{
    int N;
    cin >> N;
    vector<int> dp(MAXI + 1);
    while (N--) {
        int a;
        cin >> a;
        int m = 0;
        for (auto x : sieve.Divisors(a)) if (x < a) mmax(m, dp[x]);
        mmax(dp[a], m + 1);
    }
    cout << *max_element(dp.begin(), dp.end()) << endl;
}
0