結果

問題 No.1917 LCMST
ユーザー 👑 emthrmemthrm
提出日時 2022-04-29 22:28:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 473 ms / 4,000 ms
コード長 3,519 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 215,804 KB
実行使用メモリ 19,648 KB
最終ジャッジ日時 2023-09-11 13:49:43
合計ジャッジ時間 15,855 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,832 KB
testcase_01 AC 4 ms
5,832 KB
testcase_02 AC 4 ms
6,060 KB
testcase_03 AC 5 ms
6,008 KB
testcase_04 AC 6 ms
6,140 KB
testcase_05 AC 6 ms
6,208 KB
testcase_06 AC 7 ms
6,000 KB
testcase_07 AC 6 ms
6,140 KB
testcase_08 AC 4 ms
5,976 KB
testcase_09 AC 357 ms
19,416 KB
testcase_10 AC 358 ms
19,156 KB
testcase_11 AC 351 ms
19,276 KB
testcase_12 AC 310 ms
18,680 KB
testcase_13 AC 207 ms
14,396 KB
testcase_14 AC 325 ms
18,864 KB
testcase_15 AC 181 ms
12,968 KB
testcase_16 AC 217 ms
14,920 KB
testcase_17 AC 245 ms
16,908 KB
testcase_18 AC 265 ms
17,276 KB
testcase_19 AC 182 ms
12,008 KB
testcase_20 AC 169 ms
11,544 KB
testcase_21 AC 184 ms
12,284 KB
testcase_22 AC 168 ms
11,300 KB
testcase_23 AC 169 ms
11,372 KB
testcase_24 AC 184 ms
12,156 KB
testcase_25 AC 175 ms
11,568 KB
testcase_26 AC 177 ms
11,816 KB
testcase_27 AC 181 ms
11,884 KB
testcase_28 AC 182 ms
12,020 KB
testcase_29 AC 469 ms
19,476 KB
testcase_30 AC 473 ms
19,648 KB
testcase_31 AC 469 ms
19,408 KB
testcase_32 AC 465 ms
19,484 KB
testcase_33 AC 459 ms
19,480 KB
testcase_34 AC 97 ms
9,688 KB
testcase_35 AC 95 ms
9,780 KB
testcase_36 AC 96 ms
9,768 KB
testcase_37 AC 448 ms
19,544 KB
testcase_38 AC 442 ms
19,400 KB
testcase_39 AC 445 ms
19,404 KB
testcase_40 AC 446 ms
19,292 KB
testcase_41 AC 431 ms
19,536 KB
testcase_42 AC 414 ms
19,404 KB
testcase_43 AC 98 ms
9,904 KB
testcase_44 AC 99 ms
9,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

std::vector<int> prime_sieve(const int n, const bool get_only_prime) {
  std::vector<int> smallest_prime_factor(n + 1), prime;
  std::iota(smallest_prime_factor.begin(), smallest_prime_factor.end(), 0);
  for (int i = 2; i <= n; ++i) {
    if (smallest_prime_factor[i] == i) prime.emplace_back(i);
    for (const int p : prime) {
      if (i * p > n || p > smallest_prime_factor[i]) break;
      smallest_prime_factor[i * p] = p;
    }
  }
  return get_only_prime ? prime : smallest_prime_factor;
}

struct Divisor {
  const std::vector<int> smallest_prime_factor;

  explicit Divisor(const int n)
      : smallest_prime_factor(prime_sieve(n, false)) {}

  std::vector<int> query(int n) const {
    std::vector<int> res{1};
    while (n > 1) {
      const int prime_factor = smallest_prime_factor[n], d = res.size();
      int tmp = 1;
      for (; n % prime_factor == 0; n /= prime_factor) {
        tmp *= prime_factor;
        for (int i = 0; i < d; ++i) {
          res.emplace_back(res[i] * tmp);
        }
      }
    }
    // std::sort(res.begin(), res.end());
    return res;
  }
};

struct UnionFind {
  explicit UnionFind(const int n) : data(n, -1) {}

  int root(const int ver) {
    return data[ver] < 0 ? ver : data[ver] = root(data[ver]);
  }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool is_same(const int u, const int v) { return root(u) == root(v); }

  int size(const int ver) { return -data[root(ver)]; }

 private:
  std::vector<int> data;
};

int main() {
  constexpr int A = 100000;
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  sort(ALL(a));
  ll ans = 0;
  FOR(i, 1, n) {
    if (a[i - 1] == a[i]) ans += a[i];
  }
  a.erase(unique(ALL(a)), a.end());
  n = a.size();
  Divisor divisor(A);
  vector<int> pos[A + 1]{};
  REP(i, n) for (int j : divisor.query(a[i])) pos[j].emplace_back(i);
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> que;
  REP(i, A + 1) {
    if (pos[i].size() >= 2) {
      reverse(pos[i].begin() + 1, pos[i].end());
      que.emplace(1LL * a[pos[i].front()] * a[pos[i].back()] / i, i);
    }
  }
  UnionFind union_find(n);
  while (!que.empty()) {
    const int i = que.top().second; que.pop();
    if (union_find.unite(pos[i].front(), pos[i].back())) ans += lcm(1LL * a[pos[i].front()], a[pos[i].back()]);
    pos[i].pop_back();
    if (pos[i].size() >= 2) que.emplace(1LL * a[pos[i].front()] * a[pos[i].back()] / i, i);
  }
  cout << ans << '\n';
  return 0;
}
0