#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 1e5 + 5, M = 1e7, mod = 1e9 + 7; //!
int t, p[N], last[M + 2], id[M + 5];
vector<pair<int,int> > E[M + 5];
int find(int u) {
return (p[u] == u ? u : p[u] = find(p[u]));
}
bool merge(int u,int v) {
u = find(u), v = find(v);
if(u == v) return 0;
p[u] = v;
return 1;
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
vector<int> x;
for(int i = 1; i <= n; i++) {
int a; cin >> a;
x.push_back(a);
}
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for(int i = 0; i < x.size(); i++) {
id[x[i]] = i;
last[x[i]] = x[i];
p[i] = i;
}
for(int i = M; i >= 0; i--) {
if(!last[i]) last[i] = last[i + 1];
}
for(int i = 0; i < x.size(); i++) {
for(int j = 0; j <= M; j += x[i]) {
int y = last[j];
if(last[j] == x[i]) y = last[j + 1];
if(!y) break;
E[y % x[i]].push_back({id[x[i]], id[y]});
}
}
ll ans = 0;
for(int i = 0; i <= M; i++) {
for(int j = 0; j < E[i].size(); j++) {
int u = E[i][j].f, v = E[i][j].s;
ans += (ll)i * merge(u, v);
}
}
cout << ans;
}
Compilation message
sirni.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
20 | main() {
| ^~~~
sirni.cpp: In function 'int main()':
sirni.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
sirni.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
sirni.cpp:49:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int j = 0; j < E[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
277984 KB |
Output is correct |
2 |
Correct |
285 ms |
305968 KB |
Output is correct |
3 |
Correct |
178 ms |
278512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
274328 KB |
Output is correct |
2 |
Correct |
1641 ms |
669464 KB |
Output is correct |
3 |
Correct |
202 ms |
279056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
278348 KB |
Output is correct |
2 |
Correct |
179 ms |
276880 KB |
Output is correct |
3 |
Correct |
194 ms |
278400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
289752 KB |
Output is correct |
2 |
Correct |
375 ms |
319460 KB |
Output is correct |
3 |
Correct |
271 ms |
300124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
280368 KB |
Output is correct |
2 |
Correct |
320 ms |
300708 KB |
Output is correct |
3 |
Correct |
235 ms |
289128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
301976 KB |
Output is correct |
2 |
Correct |
431 ms |
339812 KB |
Output is correct |
3 |
Correct |
261 ms |
298820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
279300 KB |
Output is correct |
2 |
Correct |
455 ms |
336800 KB |
Output is correct |
3 |
Correct |
275 ms |
302276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
328120 KB |
Output is correct |
2 |
Correct |
1969 ms |
673436 KB |
Output is correct |
3 |
Correct |
347 ms |
331720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
333288 KB |
Output is correct |
2 |
Correct |
3775 ms |
784936 KB |
Output is correct |
3 |
Correct |
546 ms |
388016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
311140 KB |
Output is correct |
2 |
Correct |
3910 ms |
666640 KB |
Output is correct |
3 |
Correct |
301 ms |
302952 KB |
Output is correct |