結果
| 問題 |
No.1917 LCMST
|
| コンテスト | |
| ユーザー |
sotanishy
|
| 提出日時 | 2022-04-29 23:48:58 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,861 bytes |
| コンパイル時間 | 4,400 ms |
| コンパイル使用メモリ | 207,724 KB |
| 最終ジャッジ日時 | 2025-01-29 00:34:22 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 TLE * 2 MLE * 24 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }
class UnionFind {
public:
UnionFind() = default;
explicit UnionFind(int n) : data(n, -1) {}
int find(int x) {
if (data[x] < 0) return x;
return data[x] = find(data[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (data[x] > data[y]) std::swap(x, y);
data[x] += data[y];
data[y] = x;
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return -data[find(x)];
}
private:
std::vector<int> data;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int N;
cin >> N;
const int M = 1e5;
vector<ll> C(M+1);
rep(i,0,N) {
int x;
cin >> x;
++C[x];
}
ll ans = 0;
vector<ll> vs;
rep(i,1,M+1) {
if (C[i] == 0) continue;
if (C[i] > 1) {
ans += i*(C[i]-1);
C[i] = 1;
}
vs.push_back(i);
for (int j = 2*i; j <= M; j += i) {
ans += j*C[j];
C[j] = 0;
}
}
vector<tuple<ll, int, int>> es;
rep(i,0,vs.size()) rep(j,i+1,vs.size()) es.push_back({(ll)vs[i]*vs[j]/gcd(vs[i],vs[j]), i, j});
sort(all(es));
UnionFind uf(vs.size());
for (auto [v, i, j] : es) {
if (!uf.same(i, j)) ans += v;
uf.unite(i, j);
}
cout << ans << endl;
}
sotanishy