結果

問題 No.1917 LCMST
ユーザー simansiman
提出日時 2022-04-30 16:10:43
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,789 bytes
コンパイル時間 4,952 ms
コンパイル使用メモリ 108,904 KB
実行使用メモリ 13,288 KB
最終ジャッジ日時 2023-09-12 08:53:22
合計ジャッジ時間 24,728 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,396 KB
testcase_01 AC 5 ms
5,364 KB
testcase_02 AC 4 ms
5,348 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 397 ms
13,144 KB
testcase_30 AC 400 ms
13,260 KB
testcase_31 AC 400 ms
13,128 KB
testcase_32 AC 396 ms
13,128 KB
testcase_33 AC 401 ms
13,108 KB
testcase_34 AC 312 ms
13,152 KB
testcase_35 AC 311 ms
13,112 KB
testcase_36 AC 310 ms
13,100 KB
testcase_37 AC 398 ms
13,156 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;
typedef long long ll;

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);
  vector<ll> counter(100010, 0);
  ll min_a = INT_MAX;

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

  sort(A.begin(), A.end());
  vector<bool> checked(100010, false);

  ll ans = 0;
  UnionFind uf(100010);

  for (int i = 0; i < N; ++i) {
    ll a = A[i];
    if (checked[a]) continue;
    checked[a] = true;

    ans += a * (counter[a] - 1);

    for (ll x = 2 * a; x <= 100000; x += a) {
      if (checked[x]) continue;
      checked[x] = true;

      uf.unite(a, x);
      ans += x * counter[x];
    }
  }

  for (ll a : A) {
    if (uf.same(min_a, a)) continue;

    uf.unite(min_a, a);
    ans += min_a * a;
  }

  cout << ans << endl;

  return 0;
}
0