#include <bits/stdc++.h>
// author : aykhn
using namespace std;
typedef long long ll;
#define pb push_back
#define ins insert
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define bpc __builtin_popcountll
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define fi first
#define se second
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F
struct DSU
{
vector<int> e;
void init(int n)
{
e.assign(n + 1, -1);
}
int get(int x)
{
if (e[x] < 0) return x;
return e[x] = get(e[x]);
}
int tog(int x, int y)
{
return get(x) == get(y);
}
int unite(int x, int y)
{
x = get(x);
y = get(y);
if (x == y) return 0;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return 1;
}
};
const int MXN = 5e5 + 5;
vector<array<int, 2>> e[10000005];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
cin >> n;
int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
{
if (a[i] == a[i - 1]) continue;
for (int j = a[i]; j <= a[n]; j += a[i])
{
int ind = (j == a[i] ? upper_bound(a + 1, a + n + 1, j) - a : lower_bound(a + 1, a + n + 1, j) - a);
if (ind > n) break;
e[a[ind]%a[i]].pb({i, ind});
j = a[ind]/a[i]*a[i];
}
}
int res = 0;
DSU dsu;
dsu.init(n);
for (int i = 0; i < 10000005; i++)
{
for (array<int, 2> a : e[i]) if (dsu.unite(a[0], a[1])) res += i;
}
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
235344 KB |
Output is correct |
2 |
Correct |
82 ms |
237648 KB |
Output is correct |
3 |
Correct |
75 ms |
235460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
235344 KB |
Output is correct |
2 |
Correct |
76 ms |
235856 KB |
Output is correct |
3 |
Correct |
76 ms |
235376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
235420 KB |
Output is correct |
2 |
Correct |
73 ms |
235308 KB |
Output is correct |
3 |
Correct |
76 ms |
235348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
244988 KB |
Output is correct |
2 |
Correct |
406 ms |
275256 KB |
Output is correct |
3 |
Correct |
210 ms |
250232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
236884 KB |
Output is correct |
2 |
Correct |
220 ms |
257876 KB |
Output is correct |
3 |
Correct |
177 ms |
245268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
302 ms |
257368 KB |
Output is correct |
2 |
Correct |
522 ms |
291072 KB |
Output is correct |
3 |
Correct |
194 ms |
248756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
238708 KB |
Output is correct |
2 |
Correct |
463 ms |
283840 KB |
Output is correct |
3 |
Correct |
187 ms |
248164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
247824 KB |
Output is correct |
2 |
Correct |
1695 ms |
450132 KB |
Output is correct |
3 |
Correct |
212 ms |
250364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
247736 KB |
Output is correct |
2 |
Correct |
2522 ms |
535704 KB |
Output is correct |
3 |
Correct |
232 ms |
254952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
237412 KB |
Output is correct |
2 |
Correct |
2389 ms |
531076 KB |
Output is correct |
3 |
Correct |
202 ms |
250744 KB |
Output is correct |