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;
#pragma GCC optimize(2)
int nxt[10001000], p[100100],pa[100100],n,mx,ans;
vector<pair<int,int>> edges[10001000];
int abp(int n) {
return (pa[n]?pa[n]=abp(pa[n]):n);
}
bool un(int a, int b) {
a=abp(a);
b=abp(b);
if(a==b)
return 0;
pa[a]=b;
return 1;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> p[i], mx=max(mx,p[i]),nxt[p[i]]=i;
for(int i = mx; i--;)
if(!nxt[i])
nxt[i]=nxt[i+1];
for(int i = 1; i <= n; i++)
if(i-p[i-1])
for(int j = p[i]; j <= mx; j+=p[i])
edges[p[nxt[j+(j==p[i])]]%p[i]].push_back({i,nxt[j]});
for(int i = 0; i <= mx/2; i++)
for(auto [u,v]: edges[i])
if(un(u,v))
ans+=i;
cout << ans << '\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... |