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 ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;
const int N = 1e7 + 7;
int n, near[N];
vector<pa> g[N];
vector<int> val;
struct DSu {
int p[maxn];
void init() {
fill_n(p, n + 1, -1);
}
int find_node(int u) {
return p[u] < 0 ? u:p[u] = find_node(p[u]);
}
bool Union(int u, int v) {
u = find_node(u);
v = find_node(v);
if(u == v) return 0;
if(p[u] > p[v]) swap(u, v);
p[u] += p[v];
p[v] = u;
return 1;
}
void mst() {
init();
ll ans = 0;
for(int i = 0; i < val.back(); i++)
for(auto e:g[i])
if(Union(e.fi, e.se)) ans += i;
cout<<ans;
}
} ds;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n;
for(int i = 0; i < n; i++) {
int x;
cin>>x;
val.pb(x);
}
sort(all(val));
val.erase(unique(all(val)), val.end());
int j = 0;
for(int i = 1; i <= val.back(); i++) {
while(j < (int) val.size() && val[j] < i) j++;
near[i] = j;
}
for(int i = 0; i < (int) val.size() - 1; i++) {
for(int j = val[i]; j <= val.back(); j += val[i]) {
if(j != val[i]) g[val[near[j]] % j].pb({i, near[j]});
else if(j != val.back()) g[val[near[j + 1]] % j].pb({i, near[j + 1]});
}
}
ds.mst();
return 0;
}
# | 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... |