結果

問題 No.1917 LCMST
ユーザー simansiman
提出日時 2022-04-30 17:33:43
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 518 ms / 4,000 ms
コード長 2,165 bytes
コンパイル時間 5,384 ms
コンパイル使用メモリ 109,880 KB
実行使用メモリ 45,964 KB
最終ジャッジ日時 2023-09-12 10:00:58
合計ジャッジ時間 25,426 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,168 KB
testcase_01 AC 4 ms
5,256 KB
testcase_02 AC 4 ms
5,224 KB
testcase_03 AC 6 ms
5,468 KB
testcase_04 AC 6 ms
5,460 KB
testcase_05 AC 7 ms
5,480 KB
testcase_06 AC 6 ms
5,464 KB
testcase_07 AC 6 ms
5,520 KB
testcase_08 AC 4 ms
5,208 KB
testcase_09 AC 497 ms
30,004 KB
testcase_10 AC 504 ms
29,868 KB
testcase_11 AC 497 ms
29,784 KB
testcase_12 AC 461 ms
28,276 KB
testcase_13 AC 338 ms
20,280 KB
testcase_14 AC 479 ms
28,568 KB
testcase_15 AC 309 ms
16,956 KB
testcase_16 AC 340 ms
20,312 KB
testcase_17 AC 378 ms
28,492 KB
testcase_18 AC 400 ms
29,592 KB
testcase_19 AC 323 ms
16,160 KB
testcase_20 AC 306 ms
15,076 KB
testcase_21 AC 324 ms
16,392 KB
testcase_22 AC 304 ms
15,180 KB
testcase_23 AC 307 ms
14,912 KB
testcase_24 AC 324 ms
16,596 KB
testcase_25 AC 303 ms
15,128 KB
testcase_26 AC 313 ms
15,388 KB
testcase_27 AC 313 ms
16,164 KB
testcase_28 AC 318 ms
16,956 KB
testcase_29 AC 511 ms
45,120 KB
testcase_30 AC 516 ms
45,036 KB
testcase_31 AC 515 ms
44,844 KB
testcase_32 AC 511 ms
44,892 KB
testcase_33 AC 510 ms
44,700 KB
testcase_34 AC 280 ms
13,160 KB
testcase_35 AC 282 ms
13,192 KB
testcase_36 AC 281 ms
13,208 KB
testcase_37 AC 512 ms
44,912 KB
testcase_38 AC 510 ms
44,576 KB
testcase_39 AC 515 ms
45,964 KB
testcase_40 AC 514 ms
45,208 KB
testcase_41 AC 518 ms
44,572 KB
testcase_42 AC 517 ms
45,944 KB
testcase_43 AC 283 ms
13,268 KB
testcase_44 AC 284 ms
13,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <numeric>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Edge {
  int from;
  int to;
  ll cost;

  Edge(int from = -1, int to = -1, ll cost = -1) {
    this->from = from;
    this->to = to;
    this->cost = cost;
  }

  bool operator>(const Edge &n) const {
    return cost > n.cost;
  }
};

class UnionFind {
public:
  vector<int> _parent;
  vector<int> _rank;
  vector<int> _size;

  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      _parent.push_back(i);
      _rank.push_back(0);
      _size.push_back(1);
    }
  }

  int find(int x) {
    if (_parent[x] == x) {
      return x;
    } else {
      return _parent[x] = find(_parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (_rank[x] < _rank[y]) {
      _parent[x] = y;
      _size[y] += _size[x];
    } else {
      _parent[y] = x;
      _size[x] += _size[y];
      if (_rank[x] == _rank[y]) ++_rank[x];
    }
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  int size(int x) {
    return _size[find(x)];
  }
};

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  ll ans = 0;
  ll M = 0;
  vector<ll> counter(100010, 0);

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    M = max(M, A[i]);
    counter[A[i]]++;
  }

  priority_queue <Edge, vector<Edge>, greater<Edge>> pque;
  vector<bool> checked(M + 1, false);

  for (ll d = 1; d <= M; ++d) {
    if (counter[d] > 0) {
      ans += d * (counter[d] - 1);
    }
    ll min_x = -1;

    for (ll x = d; x <= M; x += d) {
      if (counter[x] == 0) continue;

      if (checked[d]) {
        pque.push(Edge(min_x, x, min_x * x / d));
      } else {
        min_x = x;
        checked[d] = true;
      }
    }
  }

  UnionFind uf(100010);

  while (not pque.empty()) {
    Edge e = pque.top();
    pque.pop();

    if (uf.same(e.from, e.to)) continue;

    uf.unite(e.from, e.to);
    ans += e.cost;
  }

  cout << ans << endl;

  return 0;
}
0