結果

問題 No.979 Longest Divisor Sequence
ユーザー hiroa
提出日時 2021-10-06 22:07:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,379 ms / 2,000 ms
コード長 1,025 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 199,868 KB
最終ジャッジ日時 2025-01-24 21:26:20
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

// 約数の列挙O(√n)
vector<ll> enum_divisors(ll n){
    vector<ll> res;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            res.push_back(i);
            // 重複しないならばiの相方であるN/iもpush
            if(i!=n/i) res.push_back(n/i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    rep(i,n) cin>>a[i];

    vector<int> ma(300010,0);
    int ans=0;
    rep(i,n){
        auto ed=enum_divisors(a[i]);
        int tmp=0;
        for(auto e : ed){
            if(e==a[i]) continue;
            tmp=max(tmp,ma[e]+1);
        }
        ma[a[i]]=tmp;
        if(a[i]==1){
            ma[1]=1;
            tmp=1;
        }
        ans=max(ans,tmp);
    }

    cout<<ans<<endl;
}


0