#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const ll N = 2000001;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
const int maxa = (int)1e7 + 1;
int a[N], n;
int best[maxa], p[N], sz[N], e[maxa];
vector<pair<int, int>> v[maxa + 1];
int f(int i, int j) {
if (a[i] > a[j]) swap(i, j);
return a[j] % a[i];
}
int find(int x) {
if (p[x] == x) return x;
return p[x] = find(p[x]);
}
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
p[b] = a;
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
fill(e, e + maxa + 1, -1);
for (int i = 0; i < n; i++) {
sz[i] = 1, p[i] = i;
}
for (int i = 0; i < n; i++) {
cin >> a[i];
e[a[i]] = i;
}
for (int i = maxa - 1; i >= 0; i--) {
if (e[i] == -1) e[i] = e[i+1];
}
for (int i = 0; i < n; i++) {
if (e[a[i]] != i) v[0].push_back({i, e[a[i]]});
if (e[a[i] + 1] != -1) {
v[f(i, e[a[i] + 1])].push_back({i, e[a[i] + 1]});
}
for (int k = 2; k * a[i] < maxa; k++) {
if (e[k * a[i]] != -1) {
v[f(i, e[k * a[i]])].push_back({i, e[k * a[i]]});
}
}
}
ll ans = 0;
for (int i = 0; i <= maxa; i++) {
for (auto& x : v[i]) {
if (unite(x.first, x.second)) {
ans += f(x.first, x.second);
//cout << a[x.first] << ' ' << a[x.second] << ' ' << f(x.first, x.second) << endl;
}
}
}
cout << ans;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:50:6: warning: array subscript 10000002 is outside array bounds of 'int [10000001]' [-Warray-bounds]
50 | fill(e, e + maxa + 1, -1);
| ~~~~^~~~~~~~~~~~~~~~~~~~~
sirni.cpp:17:30: note: while referencing 'e'
17 | int best[maxa], p[N], sz[N], e[maxa];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
274540 KB |
Output is correct |
2 |
Correct |
404 ms |
314988 KB |
Output is correct |
3 |
Correct |
235 ms |
274812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
274412 KB |
Output is correct |
2 |
Runtime error |
1856 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
274560 KB |
Output is correct |
2 |
Correct |
227 ms |
274412 KB |
Output is correct |
3 |
Correct |
236 ms |
274540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
490 ms |
285632 KB |
Output is correct |
2 |
Correct |
1259 ms |
330632 KB |
Output is correct |
3 |
Correct |
677 ms |
303024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
276588 KB |
Output is correct |
2 |
Execution timed out |
5093 ms |
467296 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
855 ms |
299896 KB |
Output is correct |
2 |
Correct |
1855 ms |
370276 KB |
Output is correct |
3 |
Correct |
574 ms |
294916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
504 ms |
278736 KB |
Output is correct |
2 |
Correct |
2015 ms |
368768 KB |
Output is correct |
3 |
Correct |
654 ms |
300904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
289004 KB |
Output is correct |
2 |
Correct |
3360 ms |
759508 KB |
Output is correct |
3 |
Correct |
445 ms |
293548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
292780 KB |
Output is correct |
2 |
Runtime error |
4485 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
276844 KB |
Output is correct |
2 |
Execution timed out |
5091 ms |
704232 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |