#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
using namespace std;
const int MAX_P = 1e7 + 1;
int cnt[MAX_P], nxt[MAX_P];
struct edge
{
int a, b, w;
};
class DSU
{
private:
vector<int> rank, par;
public:
int comps;
DSU(int n)
{
comps = n;
rank.resize(n, 0);
par.resize(n, 0);
iota(par.begin(), par.end(), 0);
}
int find(int x)
{
if (x == par[x])
return x;
return par[x] = find(par[x]);
}
bool uni(int a, int b)
{
a = find(a);
b = find(b);
if (a == b)
return false;
if (rank[b] > rank[a])
swap(a, b);
par[b] = a;
if (rank[a] == rank[b])
rank[a]++;
--comps;
return true;
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<int> arr(N);
for (int i = 0; i < N; i++)
cin >> arr[i];
sort(arr.begin(), arr.end());
arr.erase(unique(arr.begin(), arr.end()), arr.end());
N = arr.size();
map<int, int> conv;
for (int i = 0; i < N; i++)
{
conv[arr[i]] = i;
cnt[arr[i]]++;
}
int j = 0;
for (int i = 0; i < N; i++)
{
while (j < arr[i])
{
nxt[j] = arr[i];
j++;
}
}
while (j < MAX_P)
{
nxt[j] = -1;
j++;
}
vector<edge> edges;
for (int i = 1; i < MAX_P; i++)
{
if (!cnt[i])
continue;
for (int k = i; k < MAX_P; k += i)
{
if (nxt[k] == -1)
break;
if (cnt[k] > 0 && i != k)
edges.push_back({i, k, 0});
else if (k <= nxt[k] && nxt[k] < k + i)
edges.push_back({i, nxt[k], nxt[k] % k});
}
}
sort(edges.begin(), edges.end(), [&](edge &a, edge &b)
{ return a.w < b.w; });
DSU dsu(N);
ll res = 0;
for (edge &e : edges)
{
if (dsu.uni(conv[e.a], conv[e.b]))
res += e.w;
if (dsu.comps == 1)
break;
}
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
78672 KB |
Output is correct |
2 |
Correct |
47 ms |
82376 KB |
Output is correct |
3 |
Correct |
20 ms |
79100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
41944 KB |
Output is correct |
2 |
Correct |
209 ms |
79432 KB |
Output is correct |
3 |
Correct |
35 ms |
78920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
78672 KB |
Output is correct |
2 |
Correct |
18 ms |
70480 KB |
Output is correct |
3 |
Correct |
20 ms |
78684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
268 ms |
62904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
48068 KB |
Output is correct |
2 |
Correct |
169 ms |
95912 KB |
Output is correct |
3 |
Incorrect |
247 ms |
60096 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
522 ms |
99760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
87 ms |
50624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
110608 KB |
Output is correct |
2 |
Correct |
1750 ms |
479872 KB |
Output is correct |
3 |
Correct |
355 ms |
110512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
110724 KB |
Output is correct |
2 |
Incorrect |
2443 ms |
479108 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
83400 KB |
Output is correct |
2 |
Incorrect |
2561 ms |
476548 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |