結果

問題 No.1917 LCMST
ユーザー KudeKude
提出日時 2022-04-29 22:56:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,146 ms / 4,000 ms
コード長 1,959 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 236,736 KB
実行使用メモリ 91,096 KB
最終ジャッジ日時 2023-09-11 14:23:25
合計ジャッジ時間 46,096 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,428 KB
testcase_01 AC 7 ms
5,368 KB
testcase_02 AC 7 ms
5,296 KB
testcase_03 AC 13 ms
5,652 KB
testcase_04 AC 18 ms
5,696 KB
testcase_05 AC 18 ms
5,708 KB
testcase_06 AC 18 ms
5,432 KB
testcase_07 AC 18 ms
5,660 KB
testcase_08 AC 8 ms
5,360 KB
testcase_09 AC 1,670 ms
90,732 KB
testcase_10 AC 1,745 ms
90,872 KB
testcase_11 AC 2,146 ms
91,096 KB
testcase_12 AC 1,887 ms
90,588 KB
testcase_13 AC 1,526 ms
89,612 KB
testcase_14 AC 1,886 ms
90,852 KB
testcase_15 AC 1,341 ms
88,968 KB
testcase_16 AC 1,398 ms
89,840 KB
testcase_17 AC 1,587 ms
90,032 KB
testcase_18 AC 1,699 ms
90,532 KB
testcase_19 AC 1,099 ms
89,008 KB
testcase_20 AC 1,077 ms
89,004 KB
testcase_21 AC 1,195 ms
88,796 KB
testcase_22 AC 1,011 ms
88,984 KB
testcase_23 AC 1,094 ms
88,680 KB
testcase_24 AC 1,119 ms
88,980 KB
testcase_25 AC 1,139 ms
89,184 KB
testcase_26 AC 1,135 ms
90,156 KB
testcase_27 AC 1,170 ms
90,216 KB
testcase_28 AC 1,120 ms
89,576 KB
testcase_29 AC 651 ms
90,836 KB
testcase_30 AC 668 ms
91,068 KB
testcase_31 AC 647 ms
90,732 KB
testcase_32 AC 622 ms
90,936 KB
testcase_33 AC 636 ms
90,736 KB
testcase_34 AC 256 ms
87,308 KB
testcase_35 AC 225 ms
87,628 KB
testcase_36 AC 276 ms
87,380 KB
testcase_37 AC 886 ms
90,732 KB
testcase_38 AC 910 ms
90,876 KB
testcase_39 AC 877 ms
90,984 KB
testcase_40 AC 924 ms
91,056 KB
testcase_41 AC 922 ms
90,860 KB
testcase_42 AC 1,169 ms
90,860 KB
testcase_43 AC 702 ms
87,532 KB
testcase_44 AC 1,870 ms
87,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

constexpr int MX = 100010;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VI a(n);
  VVI vals(MX);
  rep(i, n) {
    cin >> a[i];
    vals[a[i]].emplace_back(i);
  }
  ll ans = 0;
  dsu uf(n);
  constexpr ll INF = 1002003004005006007;
  vector<pair<ll, pair<int, int>>> mns;
  while(true) {
    auto gs = uf.groups();
    if (gs.size() == 1) break;
    mns.assign(n, {INF, {-1, -1}});
    for(int g = 1; g < MX; g++) {
      int i1 = -1;
      int li1 = -1;
      int v = -1;
      for(int x = g; x < MX; x += g) {
        for(int i: vals[x]) {
          if (i1 == -1) {
            i1 = i;
            li1 = uf.leader(i1);
            v = a[i1] / g;
          } else if (int li = uf.leader(i); li != li1) {
            ll cost = (ll)v * a[i];
            if (cost < mns[li1].first) {
              mns[li1] = {cost, {i1, i}};
            }
            if (cost < mns[li].first) {
              mns[li] = {cost, {i1, i}};
            }
          }
        }
      }
    }
    rep(l, n) if (mns[l].first != INF) {
      auto [cost, ij] = mns[l];
      auto [i, j] = ij;
      if (!uf.same(i, j)) {
        ans += cost;
        uf.merge(i, j);
      }
    }
  }
  cout << ans << endl;
}
0