#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
const int N = 1e5 + 5, M = 1e7 + 5, INF = 1e9;
int q[N], w[M], nt[M], c[M], st[N];
pair<int, int> mn[N];
bool b[M], d[M];
int findSet(int v) {
return q[v] < 0 ? v : q[v] = findSet(q[v]);
}
bool connect(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return false;
if (q[a] > q[b])
swap(a, b);
q[a] += q[b];
q[b] = a;
return true;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
b[x] = 1;
}
fill(w, w + M, -1);
fill(q, q + N, -1);
vector<int> v;
int cnt = 0;
for (int i = 0; i < M; i++) {
if (b[i]) {
w[i] = cnt++;
}
}
nt[M - 1] = INF;
for (int i = M - 2; i >= 0; i--) {
nt[i] = b[i] ? i : nt[i + 1];
}
int remain = cnt;
for (int i = 0; i < M; i++) {
if (b[i]) {
for (int j = i + i; j < M; j += i) {
if (b[j] && connect(w[i], w[j])) {
remain--;
}
d[j] = 1;
}
}
}
for (int i = 0; i < M; i++) {
if (b[i] && !d[i])
v.push_back(i);
}
ll ans = 0;
while (remain > 1) {
for (int i = 0; i < cnt; i++)
mn[i] = { INF, -1 };
for (int x : v) {
int cur_set = st[w[x]];
for (int i = x; i < M - 1; i += x) {
int j = nt[i + 1];
if (j < i + x) {
int oth_set = st[w[j]];
if (oth_set != cur_set) {
if (mn[cur_set].first > j - i)
mn[cur_set] = { j - i, oth_set };
if (mn[oth_set].first > j - i)
mn[oth_set] = { j - i, cur_set };
}
}
}
}
for (int i = 0; i < cnt; i++) {
if (q[i] < 0 && mn[i].second != -1 && connect(i, mn[i].second)) {
ans += mn[i].first;
remain--;
}
}
for (int i = 0; i < cnt; i++) {
st[i] = findSet(i);
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
90360 KB |
Output is correct |
2 |
Correct |
418 ms |
90744 KB |
Output is correct |
3 |
Correct |
142 ms |
92024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
88824 KB |
Output is correct |
2 |
Correct |
414 ms |
89336 KB |
Output is correct |
3 |
Correct |
143 ms |
92124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
90924 KB |
Output is correct |
2 |
Correct |
136 ms |
85468 KB |
Output is correct |
3 |
Correct |
141 ms |
92176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
670 ms |
91244 KB |
Output is correct |
2 |
Correct |
1051 ms |
91100 KB |
Output is correct |
3 |
Correct |
595 ms |
91040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
90064 KB |
Output is correct |
2 |
Correct |
1061 ms |
89988 KB |
Output is correct |
3 |
Correct |
891 ms |
90532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1073 ms |
91136 KB |
Output is correct |
2 |
Correct |
879 ms |
91080 KB |
Output is correct |
3 |
Correct |
440 ms |
91120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
627 ms |
89596 KB |
Output is correct |
2 |
Correct |
1114 ms |
90824 KB |
Output is correct |
3 |
Correct |
528 ms |
91064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
379 ms |
100464 KB |
Output is correct |
2 |
Correct |
1048 ms |
100296 KB |
Output is correct |
3 |
Correct |
437 ms |
100396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
100812 KB |
Output is correct |
2 |
Correct |
1685 ms |
100256 KB |
Output is correct |
3 |
Correct |
357 ms |
100392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
99328 KB |
Output is correct |
2 |
Correct |
1909 ms |
100220 KB |
Output is correct |
3 |
Correct |
560 ms |
91384 KB |
Output is correct |