#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct DSU{
int n;
vector<int> parent;
vector<int> sz;
void init(int nn){
n = nn;
parent.resize(n);
sz.resize(n);
for (int i=0; i<n; i++){
parent[i] = i;
sz[i] = 1;
}
}
int find(int a){
if (parent[a] == a){
return a;
}
return parent[a] = find(parent[a]); // Path compression
}
void join(int a, int b){
a = find(a);
b = find(b);
if (a == b){
// Same component
return;
}
if (sz[a] < sz[b]){
// Point a to b
parent[a] = b;
sz[b] += sz[a];
}else{
// Point b to a
parent[b] = a;
sz[a] += sz[b];
}
}
};
const int MAXN = 1e5 + 5, MAXM = 1e7 + 5;
int arr[MAXN];
vector<array<int, 3>> edges;
int n, maxM = 0;
int bs(int target){
int low = 0;
int high = n-1;
while (low < high){
int mid = (low + high)/2;
if (arr[mid] >= target){
high = mid;
}else{
low = mid+1;
}
}
if (arr[low] < target){
return -1;
}
return arr[low];
}
void findMultiples(int base){
int k = 1;
while (base*k <= maxM){
if (k == 1){
int val = bs(base*k+1);
if (val != -1){
edges.push_back({base, val, val % base});
}
}else{
int val = bs(base*k);
if (val != -1){
edges.push_back({base, val, val % base});
}
}
k++;
}
}
bool cmp(array<int, 3> a, array<int, 3> b){
return a[2] < b[2];
}
int main(){
cin.tie(0)->sync_with_stdio(0);
//freopen("file.in", "r", stdin);
//freopen("Sirni.out", "w", stdout);
cin >> n;
for (int i=0; i<n; i++){
cin >> arr[i];
maxM = max(maxM, arr[i]);
}
sort(arr, arr + n);
int prev = -1;
for (int i : arr){
if (i > prev){
findMultiples(i);
prev = i;
}
}
sort(edges.begin(), edges.end(), cmp);
DSU dsu;
dsu.init(maxM+1);
int ans = 0;
for (auto curr : edges){
if (dsu.find(curr[0]) != dsu.find(curr[1])){
dsu.join(curr[0], curr[1]);
ans += curr[2];
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
78668 KB |
Output is correct |
2 |
Correct |
408 ms |
110820 KB |
Output is correct |
3 |
Correct |
52 ms |
78656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Runtime error |
1400 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
78668 KB |
Output is correct |
2 |
Correct |
59 ms |
78472 KB |
Output is correct |
3 |
Correct |
54 ms |
78836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
250 ms |
26092 KB |
Output is correct |
2 |
Correct |
885 ms |
58232 KB |
Output is correct |
3 |
Correct |
444 ms |
50664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
10552 KB |
Output is correct |
2 |
Correct |
491 ms |
50504 KB |
Output is correct |
3 |
Correct |
247 ms |
25952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
50680 KB |
Output is correct |
2 |
Correct |
1316 ms |
99880 KB |
Output is correct |
3 |
Correct |
403 ms |
31704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
6908 KB |
Output is correct |
2 |
Correct |
1262 ms |
99920 KB |
Output is correct |
3 |
Correct |
396 ms |
33056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
387 ms |
95776 KB |
Output is correct |
2 |
Runtime error |
2410 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
445 ms |
101932 KB |
Output is correct |
2 |
Runtime error |
2331 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
81124 KB |
Output is correct |
2 |
Runtime error |
3008 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |