#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 1e5;
const int NUMS = 1e7;
int par[NUMS+1], sz[NUMS+1], cnt[NUMS+1], up[NUMS+1], Divs[NUMS+1];
int find_set(int v){
if(v == par[v]) return v;
return par[v] = find_set(par[v]);
}
long long ans;
void union_set(int a, int b, int c){
// cout << a << ' ' << b << ' ' << c << '\n';
a = find_set(a);
b = find_set(b);
if(a == b) return;
ans+= c;
sz[a]+= sz[b];
par[b] = a;
}
int a[N+1], n;
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
vector<int> v;
for(int i = 1;i <= n; i++){
cin >> a[i];
v.push_back(a[i]);
}
for(int i = 1;i <= NUMS; i++){
par[i] = i, sz[i] = 1;
}
sort(all(v));
v.erase(unique(all(v)), v.end());
for(auto x : v) cnt[x] = 1;
for(int i = 1;i < v[0]; i++) up[i] = v[0];
for(int i = 1; i < v.size(); i++){
for(int k = v[i-1]; k < v[i]; k++) up[k] = v[i];
}
if(cnt[1]){
cout << 0;
return 0;
}
vector< pair<int, pair<int,int>>> edge;
for(int i = 1;i <= NUMS; i++){
if(!cnt[i]) continue;
if(Divs[i]) continue;
for(int j = i; j <= NUMS; j+= i){
if(cnt[j]) union_set(i, j, 0);
if(up[j] != 0 and up[j] < (j+i)){
if(cnt[up[j]])
edge.push_back({up[j]%i, {i, up[j]}});
}
Divs[j] = i;
}
}
sort(all(edge));
for(auto [x, u] : edge){
union_set(u.ff, u.ss, x);
}
cout << ans;
return 0;
}
Compilation message
sirni.cpp:27:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
27 | main(){
| ^~~~
sirni.cpp: In function 'int main()':
sirni.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 1; i < v.size(); i++){
| ~~^~~~~~~~~~
sirni.cpp:65:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for(auto [x, u] : edge){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
137776 KB |
Output is correct |
2 |
Correct |
155 ms |
161804 KB |
Output is correct |
3 |
Correct |
93 ms |
158140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
118064 KB |
Output is correct |
2 |
Correct |
172 ms |
157536 KB |
Output is correct |
3 |
Correct |
71 ms |
161008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
138976 KB |
Output is correct |
2 |
Correct |
59 ms |
127768 KB |
Output is correct |
3 |
Correct |
67 ms |
148164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
139100 KB |
Output is correct |
2 |
Correct |
444 ms |
151452 KB |
Output is correct |
3 |
Correct |
394 ms |
151524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
127440 KB |
Output is correct |
2 |
Correct |
508 ms |
150692 KB |
Output is correct |
3 |
Correct |
346 ms |
135208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
507 ms |
151476 KB |
Output is correct |
2 |
Correct |
380 ms |
151472 KB |
Output is correct |
3 |
Correct |
279 ms |
139124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
122940 KB |
Output is correct |
2 |
Correct |
411 ms |
151460 KB |
Output is correct |
3 |
Correct |
342 ms |
139180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
335 ms |
221712 KB |
Output is correct |
2 |
Correct |
1310 ms |
295608 KB |
Output is correct |
3 |
Correct |
372 ms |
221732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
221788 KB |
Output is correct |
2 |
Correct |
1470 ms |
294492 KB |
Output is correct |
3 |
Correct |
431 ms |
221772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
194308 KB |
Output is correct |
2 |
Correct |
1929 ms |
386244 KB |
Output is correct |
3 |
Correct |
362 ms |
151460 KB |
Output is correct |