#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], r_nt[M], c[M];
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;
mt19937 rnd(time(0));
for (int i = 0; i < n; i++) {
int x;
cin >> x;
// x = rnd() % 10000000 + 1;
b[x] = 1;
}
fill(w, w + M, -1);
fill(c, c + M, -1);
fill(q, q + N, -1);
vector<int> v;
for (int i = 0; i < M; i++) {
if (b[i]) {
w[i] = v.size();
v.push_back(i);
}
}
for (int i = 0; i < v.size(); i++) {
for (int j = v[i]; j < M; j += v[i]) {
c[j] = i;
}
}
int cnt = v.size();
for (int i = 0; i < v.size(); i++) {
for (int j = v[i] + v[i]; j < M; j += v[i]) {
if (b[j]) {
if (connect(i, w[j])) {
cnt--;
}
d[j] = 1;
}
}
}
nt[M - 1] = INF;
for (int i = M - 2; i >= 0; i--) {
nt[i] = b[i] ? i : nt[i + 1];
}
r_nt[0] = -1;
for (int i = 1; i < M; i++) {
r_nt[i] = c[i] != -1 ? i : r_nt[i - 1];
}
// for (int i = 0; i < 20; i++) {
// cout << nt[i] << " ";
// }
// cout << "\n";
// for (int i = 0; i < 20; i++) {
// cout << w[i] << " ";
// }
// cout << "\n";
ll ans = 0;
while (cnt > 1) {
for (int i = 0; i < v.size(); i++)
mn[i] = { INF, -1 };
for (int k = 0; k < v.size(); k++) {
if (d[v[k]])
continue;
int x = v[k], cur_set = findSet(k);
for (int i = x; i < M - 1; i += x) {
int j = x == i ? nt[i + 1] : nt[i];
if (j < i + x) {
int oth_set = findSet(w[j]);
if (oth_set != cur_set) {
mn[cur_set] = min(mn[cur_set], { j - i, oth_set });
mn[oth_set] = min(mn[oth_set], { j - i, cur_set });
}
}
}
if (r_nt[x - 1] != -1) {
int bef = r_nt[x - 1], oth_set = findSet(c[bef]);
if (oth_set != cur_set) {
mn[cur_set] = min(mn[cur_set], { x - bef, oth_set });
mn[oth_set] = min(mn[oth_set], { x - bef, cur_set });
}
}
}
for (int i = 0; i < v.size(); i++) {
if (q[i] < 0 && mn[i].second != -1 && connect(i, mn[i].second)) {
ans += mn[i].first;
cnt--;
}
}
}
cout << ans;
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:65:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); i++) {
~~^~~~~~~~~~
sirni.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); i++) {
~~^~~~~~~~~~
sirni.cpp:110:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); i++)
~~^~~~~~~~~~
sirni.cpp:114:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int k = 0; k < v.size(); k++) {
~~^~~~~~~~~~
sirni.cpp:140:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); i++) {
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
160504 KB |
Output is correct |
2 |
Correct |
544 ms |
159736 KB |
Output is correct |
3 |
Correct |
212 ms |
160576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
477 ms |
157424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
160632 KB |
Output is correct |
2 |
Correct |
227 ms |
158552 KB |
Output is correct |
3 |
Correct |
208 ms |
160604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1041 ms |
160592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
366 ms |
159584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1896 ms |
160520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
951 ms |
158508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1278 ms |
178004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
992 ms |
178108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
406 ms |
168788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |