結果

問題 No.979 Longest Divisor Sequence
ユーザー yuji9511yuji9511
提出日時 2020-01-31 22:30:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,401 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 1,847 ms
コンパイル使用メモリ 174,604 KB
実行使用メモリ 24,028 KB
最終ジャッジ日時 2023-10-17 10:43:45
合計ジャッジ時間 4,868 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
16,884 KB
testcase_01 AC 6 ms
16,888 KB
testcase_02 AC 6 ms
16,884 KB
testcase_03 AC 5 ms
16,888 KB
testcase_04 AC 6 ms
16,884 KB
testcase_05 AC 6 ms
16,888 KB
testcase_06 AC 6 ms
16,888 KB
testcase_07 AC 6 ms
16,888 KB
testcase_08 AC 6 ms
16,888 KB
testcase_09 AC 7 ms
16,888 KB
testcase_10 AC 19 ms
16,984 KB
testcase_11 AC 20 ms
16,988 KB
testcase_12 AC 21 ms
16,988 KB
testcase_13 AC 41 ms
22,208 KB
testcase_14 AC 1,401 ms
24,028 KB
testcase_15 AC 461 ms
19,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
struct Integer{
public:
    ll gcd(ll a,ll b){return b ? gcd(b, a % b) : a;}
    ll lcm(ll a,ll b){return a / gcd(a, b) * b;}

    bool isPrime(ll x){
        if(x < 2) return false;
        if(x == 2) return true;
        if(x % 2 == 0) return false;
        for(ll i = 3;i <= sqrt(x) + 1;i += 2) if(x % i == 0) return false;
        return true;
    }

    vector<ll> divisor(ll M){
        vector<ll> dd;
        for(ll i = 1; i * i <= M; i++){
            if(M % i == 0){
                dd.push_back(i);
                if(i * i != M) dd.push_back(M / i);
            }
        }
        sort(dd.begin(), dd.end());
        return dd;
    }

    vector<ll> factor(ll M){ //素因数分解
        vector<ll> dd;
        if(M == 1){
            dd.push_back(1);
            return dd;
        }
        for(ll i = 2; i*i <= M; i++){
            while(M % i == 0){
                dd.push_back(i);
                M /= i;
            }
        }
        if(M != 1) dd.push_back(M);
        sort(dd.begin(), dd.end());
        return dd;
    }
};
ll A[300010];

ll cnt[300010] = {};
vector<ll> pos[300010];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    rep(i,0,N) cin >> A[i];
    Integer it;
    ll ans = 0;
    ll dp[300010] = {};
    rep(i,0,N){
        pos[A[i]].push_back(i);
    }

    rep(i,0,N){
        dp[i] = 1;
        vector<ll> dd = it.divisor(A[i]);
        for(auto &e: dd){
            if(e == A[i]) continue;
            ll ps = lower_bound(pos[e].begin(), pos[e].end(), i) - pos[e].begin();
            if(ps != 0){
                ps = pos[e][ps-1];
                dp[i] = max(dp[i], dp[ps]+1);
            }
        }

    }
    rep(i,0,N) ans = max(ans, dp[i]);
    print(ans);

    

}
0