# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
989542 | re4ler | Sirni (COCI17_sirni) | C++17 | 2266 ms | 476300 KiB |
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;
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define pb push_back
#define getbit(x , i) ((x >> i) & 1)
typedef pair<int , int> pii;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int N = 1e7 + 5;
int n;
int a[maxn] , cha[maxn];
bool dd[N];
struct edge{
int u , v , w;
};
edge e[50000005];
bool operator <(const edge &x , const edge &y){
return x.w < y.w;
}
int find(int u){
if(cha[u] != u) cha[u] = find(cha[u]);
return cha[u];
}
void join(int u , int v){
cha[u] = v;
}
main()
{
faster
cin >> n;
vector<int> b;
b.pb(0);
for(int i = 1 ; i <= n ; ++i){
cin >> a[i];
b.pb(a[i]);
}
sort(b.begin() , b.end());
auto id = unique(b.begin() , b.end());
b.resize(distance(b.begin() , id));
for(int i = 1 ; i < b.size() ; ++i) cha[i] = i;
int ptr = 0;
for(int i = 1 ; i < b.size() ; ++i){
if(dd[b[i]]) continue;
for(int j = b[i] ; j <= 1e7 ; j += b[i]){
dd[j] = 1;
int p = lower_bound(b.begin() , b.end() , j) - b.begin();
if(p < b.size()) e[++ptr] = {i , p , min(b[i] % b[p] , b[p] % b[i])};
if(p + 1 < b.size()) e[++ptr] = {i , p + 1 , min(b[i] % b[p + 1] , b[p + 1] % b[i])};
}
}
sort(e + 1 , e + 1 + ptr);
int ans = 0;
for(int i = 1 ; i <= ptr ; ++i){
int u1 = find(e[i].u);
int v1 = find(e[i].v);
if(u1 == v1) continue;
join(u1 , v1);
ans += e[i].w;
}
cout << ans;
}
Compilation message (stderr)
# | 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... |