結果
問題 |
No.1917 LCMST
|
ユーザー |
|
提出日時 | 2022-04-30 02:07:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 268 ms / 4,000 ms |
コード長 | 1,534 bytes |
コンパイル時間 | 2,366 ms |
コンパイル使用メモリ | 211,484 KB |
最終ジャッジ日時 | 2025-01-29 00:46:54 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include <bits/stdc++.h> using namespace std; #ifdef _RUTHEN #include "debug.hpp" #else #define show(...) true #endif using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T> using V = vector<T>; #include <atcoder/dsu> int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; V<ll> A(N); rep(i, N) cin >> A[i]; const int M = 100000; V<ll> cnt(M + 1, 0); rep(i, N) cnt[A[i]]++; ll ans = 0; V<ll> B; for (int i = 1; i <= M; i++) { if (cnt[i] > 0) { ans += i * (cnt[i] - 1); cnt[i] = 1; B.push_back(i); } } V<int> Bid(M + 1, -1); int N2 = B.size(); rep(i, N2) Bid[B[i]] = i; V<tuple<ll, int, int>> es; for (int d = 1; d <= M; d++) { // gcd=dと決め打ってしまう(実際にはgcdがdより大きくなる場合、コストが小さくなるだけなので最小のものを考えているときには問題ない) // lcm(a,b)=a*b/d V<int> id; for (int j = d; j <= M; j += d) { if (cnt[j]) { id.push_back(Bid[j]); } } for (int i = 1; i < id.size(); i++) { es.push_back({B[id[0]] * B[id[i]] / d, id[0], id[i]}); } } atcoder::dsu uf(N2); sort(es.begin(), es.end()); for (auto [cost, a, b] : es) { if (!uf.same(a, b)) { uf.merge(a, b); ans += cost; } } cout << ans << '\n'; return 0; }