結果

問題 No.979 Longest Divisor Sequence
ユーザー hiroahiroa
提出日時 2021-10-06 22:07:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,385 ms / 2,000 ms
コード長 1,025 bytes
コンパイル時間 2,213 ms
コンパイル使用メモリ 206,236 KB
実行使用メモリ 5,732 KB
最終ジャッジ日時 2023-09-30 08:57:20
合計ジャッジ時間 5,439 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,412 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 16 ms
4,376 KB
testcase_11 AC 17 ms
4,376 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 58 ms
5,732 KB
testcase_14 AC 1,385 ms
5,352 KB
testcase_15 AC 464 ms
4,560 KB
権限があれば一括ダウンロードができます

ソースコード

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