結果

問題 No.979 Longest Divisor Sequence
ユーザー koi_kotyakoi_kotya
提出日時 2020-01-31 22:14:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 2,021 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 177,700 KB
実行使用メモリ 6,156 KB
最終ジャッジ日時 2023-10-17 10:08:00
合計ジャッジ時間 3,151 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)


int const MAX_N = 300010;
vector<ll> prime;
vector<bool> is_prime(MAX_N,true);
void Eratosthenes() {
    is_prime[0] = is_prime[1] = false;
    for (ll i = 2;i*i < MAX_N;++i) if (is_prime[i]) for (ll j = 2*i;j < MAX_N;j += i) is_prime[j] = false;
    for (ll i = 0;i < MAX_N;++i) if (is_prime[i]) prime.push_back(i);
}

vector<P> prime_factorization(ll a) {
    vector<P> prime_factor;
    if (is_prime[0]) Eratosthenes();
    for (ll& i : prime) {
        if (i*i > a) break;
        if (a%i == 0) {
            P p = P(i,0);
            while (a%i == 0) {
                a /= i;
                p.second++;
            }
            prime_factor.push_back(p);
        }
    }
    if (a != 1) prime_factor.push_back(P(a,1));
    return prime_factor;
}

void rec(vector<ll>& div,vector<P>& fact,int i,ll d) {
    if (i == fact.size()) {
        div.push_back(d);
        return;
    }
    for (int j = 0;j <= fact[i].second;++j) {
        rec(div,fact,i+1,d);
        d *= fact[i].first;
    }
}

// unsorted
vector<ll> divisor(ll a) {
    vector<ll> div;
    vector<P> prime_factor = prime_factorization(a);
    rec(div,prime_factor,0,1);
    return div;
}


int main() {
    int n;
    cin >> n;
    vector<int> a(n),b(300010,0);
    for (int i = 0;i < n;++i) scanf("%d",&a[i]);
    int ans = 0;
    for (int i = n-1;i >= 0;--i) {
        vector<ll> d = divisor(a[i]);
        for (ll& j : d) if (j != a[i]) b[j] = max(b[j],b[a[i]]+1);
        if (a[i] == 1) ans = max(ans,b[1]+1);
    }
    cout << max(ans,*max_element(b.begin(),b.end())) << endl;
    return 0;
}
0