This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define task "ROADS"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vl;
template <typename T> inline void read (T &x) {bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-');
if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); }
const int N = 1e5 + 1, NN = 1e7 + 1;
int n, near[NN], id[NN], rt[N], rk[N];
vi com;
vii edge[NN];
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
int main()
{
//freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(nullptr);
read(n);
rep(i, 1, n)
{
int p;
read(p);
com.eb(p);
}
sort(all(com));
com.erase(unique(all(com)), com.end());
rep(i, 0, SZ(com) - 1) id[com[i]] = i;
for (int v : com) near[v] = v;
Rep(i, com.back() - 1, 1) if (!near[i]) near[i] = near[i + 1];
rep(i, 0, SZ(com) - 2)
{
for (int j = com[i]; j <= com.back(); j += com[i])
{
int ne = (j == com[i] ? near[j + 1] : near[j]);
if (ne == 0) break;
if (ne >= com[i] + j) j = ne / com[i] * com[i], ne = near[j];
if (ne - j < com[0]) edge[ne - j].pb({i, id[ne]});
}
}
rep(i, 1, SZ(com) - 1) rt[i] = i;
int cnt = 0, ans = 0;
rep(i, 0, NN - 2) for (const ii &E : edge[i])
{
int x = frt(E.F), y = frt(E.S);
if (x != y)
{
if (rk[x] == rk[y]) rk[x]++;
if (rk[x] < rk[y]) swap(x, y);
rt[y] = x;
cnt++;
ans += i;
if (cnt == SZ(com) - 1)
{
wrip(ans);
return 0;
}
}
}
return 0;
}
Compilation message (stderr)
sirni.cpp: In function 'void read(T&)':
sirni.cpp:40:1: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
40 | if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
| ^~
sirni.cpp:40:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
40 | if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |