結果

問題 No.979 Longest Divisor Sequence
ユーザー shop_oneshop_one
提出日時 2020-01-31 22:56:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,489 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 1,134 ms
コンパイル使用メモリ 89,832 KB
実行使用メモリ 21,704 KB
最終ジャッジ日時 2023-10-17 11:43:42
合計ジャッジ時間 4,079 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
15,192 KB
testcase_01 AC 8 ms
15,192 KB
testcase_02 AC 8 ms
15,192 KB
testcase_03 AC 8 ms
15,192 KB
testcase_04 AC 8 ms
15,192 KB
testcase_05 AC 8 ms
15,192 KB
testcase_06 AC 8 ms
15,192 KB
testcase_07 AC 8 ms
15,192 KB
testcase_08 AC 8 ms
15,192 KB
testcase_09 AC 8 ms
15,192 KB
testcase_10 AC 22 ms
15,192 KB
testcase_11 AC 22 ms
15,192 KB
testcase_12 AC 23 ms
15,192 KB
testcase_13 AC 43 ms
20,472 KB
testcase_14 AC 1,489 ms
21,704 KB
testcase_15 AC 482 ms
18,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
#define MAX_N 310000
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
vector<ll> a(MAX_N),dp(MAX_N),idx[MAX_N];
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(10);
}
vector< ll > divisor(ll n) {
  vector< ll > ret;
  for(ll i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
signed main(){
  init_io();
  ll n;
  cin >> n;
  dp[0] = 1;
  for(int i=0;i<n;i++){
    cin >> a[i];
    idx[a[i]].push_back(i);
  }
  ll ans = 1;
  vector<ll> divi;
  for(ll i=1;i<n;i++){
    divi = divisor(a[i]);
    ll great = 0;
    for(auto v:divi){
      if(idx[v].size()==0)continue;
      if(v==a[i]) continue;
      auto itr = lower_bound(idx[v].begin(),idx[v].end(),i);
      if(itr==idx[v].begin()&&*itr>=i) continue;
      itr--;
      ll d = *itr;
      //cout << d<<" "<<v<<" "<<i<<endl;
      great = max(great,dp[d]);
    }
    dp[i] = great+1;
    ans = max(dp[i],ans);
    divi.clear();
  }
  cout << ans << endl;
}
0