結果
| 問題 | No.3827 MST on LCM Graph |
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2026-11-22 14:30:46 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 135 ms / 2,000 ms |
| + 356µs | |
| コード長 | 3,968 bytes |
| 記録 | |
| コンパイル時間 | 3,050 ms |
| コンパイル使用メモリ | 370,352 KB |
| 実行使用メモリ | 25,656 KB |
| 最終ジャッジ日時 | 2026-11-22 14:30:54 |
| 合計ジャッジ時間 | 6,851 ms |
|
ジャッジサーバーID (参考情報) |
judge6_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
pair<vector<int>, vector<int>> primes_lpf(const int n) {
vector<int> primes; primes.reserve(n / 10);
vector<int> lpf(n + 1);
for (int i = 2; i <= n; i += 2) lpf[i] = 2;
for (int i = 3; i <= n; i += 6) lpf[i] = 3;
if (2 <= n) primes.push_back(2);
if (3 <= n) primes.push_back(3);
// 5 * x <= n, x <= floor(n / 5)
const int n5 = n / 5;
int x = 5;
char add_next = 2;
for (; x <= n5; x += add_next, add_next ^= 0x2 ^ 4) {
int px = lpf[x];
if (px == 0) {
lpf[x] = px = x;
primes.push_back(x);
}
for (int i = 2;; ++i) {
int q = primes[i];
int y = q * x;
if (y > n) break;
lpf[y] = q;
if (q == px) break;
}
}
for (; x <= n; x += add_next, add_next ^= 0x2 ^ 4) {
if (lpf[x] == 0) {
lpf[x] = x;
primes.push_back(x);
}
}
return {move(primes), move(lpf)};
}
constexpr int PSIZE = 1000000;
auto [primes, lpf] = primes_lpf(PSIZE);
vector<int>& divisors_lv(int x) {
int ps[10], cs[10];
int sz = 0;
while (x != 1) {
int p = lpf[x], c = 0;
do {x /= p; c++;} while (x % p == 0);
ps[sz] = p; cs[sz] = c; sz++;
}
static vector<int> res;
res.clear();
res.emplace_back(1);
for (int i = 0; i < sz; i++) {
int p = ps[i], ptr = 0;
for (int t = cs[i] * res.size(); t > 0; t--)
res.emplace_back(res[ptr++] * p);
}
return res;
}
template <int k, class I, class L, class Comp=less<I>>
struct TopK {
int sz = 0;
pair<I, L> v[k];
void add(I value, L label) {
if (auto it = ranges::find(v, v + sz, label, &pair<I, L>::second); it != v + sz) {
if (Comp{}(value, it->first)) {
auto ins = ranges::upper_bound(v, it, value, Comp{}, &pair<I, L>::first);
move_backward(ins, it, it + 1);
*ins = pair(move(value), move(label));
}
return;
}
auto ins = ranges::upper_bound(v, v + sz, value, Comp{}, &pair<I, L>::first);
if (ins == v + k) return;
auto it = sz == k ? v + k - 1 : v + sz++;
move_backward(ins, it, it + 1);
*ins = pair(move(value), move(label));
}
auto begin() { return v; }
auto end() { return v + sz; }
auto& operator[](int i) { return v[i]; }
};
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
VI a;
ll ans = 0;
rep(i, n) {
int x;
cin >> x;
static bool used[200010];
if (used[x]) {
ans += x;
} else {
used[x] = true;
a.emplace_back(x);
}
}
n = a.size();
dsu uf(n);
vector<TopK<2, int, int>> cand(200010);
while (uf.size(0) != n) {
rep(i, 200010) cand[i].sz = 0;
rep(i, n) {
int l = uf.leader(i);
for (int d : divisors_lv(a[i])) {
cand[d].add(a[i], l);
}
}
constexpr long long INF = 1002003004005006007;
vector<tuple<int, int, ll>> todo;
for (auto& g : uf.groups()) {
int l = uf.leader(g[0]);
ll mn = INF;
int lopt = -1;
for (int i : g) for (int d : divisors_lv(a[i])) {
for (auto [val, lb] : cand[d]) if (lb != l) {
if (chmin(mn, (ll)val * a[i] / d)) lopt = lb;
break;
}
}
if (lopt != -1 && !uf.same(l, lopt)) {
ans += mn;
uf.merge(l, lopt);
}
}
}
cout << ans << '\n';
}
Kude