#include <stdio.h>
#include <string.h>
#define N 100000
#define A 10000000
#define M 50000000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int ds[A + 1];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
int join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return 0;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
return 1;
}
int main() {
static char used[A + 1];
static int next[A + 1], kk[A + 1], aa[M], bb[M], ww[M], hh[M];
int n, m, h, h_, a, b, ans;
scanf("%d", &n);
while (n--) {
scanf("%d", &a);
used[a] = 1;
}
for (a = A; a >= 0; a--)
next[a] = used[a] ? a : (a == A ? INF : next[a + 1]);
m = 0;
for (a = 1; a < A; a++) {
if (!used[a])
continue;
for (b = a; b <= A; b += a) {
int c = next[b == a ? a + 1 : b];
if (c == b)
used[c] = 0;
if (c <= A && c < b + a) {
aa[m] = a, bb[m] = c, ww[m] = c - b, m++;
kk[ww[m - 1]]++;
}
}
}
for (a = 1; a <= A; a++)
kk[a] += kk[a - 1];
for (h = 0; h < m; h++)
hh[kk[ww[h] - 1]++] = h;
memset(ds, -1, (A + 1) * sizeof *ds);
ans = 0;
for (h = 0; h < m; h++) {
h_ = hh[h];
if (join(aa[h_], bb[h_]))
ans += ww[h_];
}
printf("%d\n", ans);
return 0;
}
Compilation message
sirni.c: In function 'main':
sirni.c:38:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
sirni.c:40:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", &a);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
120856 KB |
Output is correct |
2 |
Correct |
168 ms |
122448 KB |
Output is correct |
3 |
Correct |
117 ms |
121140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
159 ms |
117828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
121048 KB |
Output is correct |
2 |
Correct |
112 ms |
118744 KB |
Output is correct |
3 |
Correct |
110 ms |
121236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
253 ms |
129760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
139 ms |
120700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
385 ms |
138368 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
230 ms |
121092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
284 ms |
145308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
273 ms |
144104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
136 ms |
130264 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |