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>
using namespace std;
const int N = 1e7+10;
int suff[N];
int pos[N];
struct DSU {
vector<int> e;
DSU(int n) {
e=vector<int>(n,-1);
}
int get(int a) {
if(e[a] < 0)return a;
return e[a]=get(e[a]);
}
int unite(int a, int b) {
a=get(a),b=get(b);
if(a==b)return 0;
if(-e[a] > -e[b])swap(a,b);
e[b]+=e[a];
e[a] = b;
return 1;
}
};
int main () {
memset(suff, -1, sizeof suff);
int n;
cin >> n;
set<int> s;
for(int i = 0;i<n;i++) {
int v;
cin >> v;
s.insert(v);
}
vector<int> v(s.begin(), s.end());
{
int cnt=0;
for(int i:v) {
suff[i] =i;
pos[i] = cnt++;
}
}
for(int i = N-2;i>=0;i--) {
if(suff[i] == -1)suff[i] = suff[i+1];
}
vector<pair<int,int>> e[N/2];
for(int i:v) {
for(int j = i;j<N;j+=i) {
if(suff[j] == j) {
e[0].push_back({j, i});
if(suff[j+1]!=-1) {
if(suff[j+1]-j < N/2)
e[suff[j+1]-j].push_back({suff[j+1], i});
}
}
else if(suff[j]!=-1){
if(suff[j]-j < N/2)
e[suff[j]-j].push_back({suff[j], i});
}
}
}
long long sum=0;
DSU d(n);
for(int i = 0;i<N/2;i++) {
for(auto [u, x]:e[i]) {
sum+=i*d.unite(pos[u], pos[x]);
}
}
cout << sum << "\n";
}
# | 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... |