Submission #954594

# Submission time Handle Problem Language Result Execution time Memory
954594 2024-03-28T07:58:07 Z HuyAT Sirni (COCI17_sirni) C++17
0 / 140
290 ms 335048 KB
#include<bits/stdc++.h>

const int MaxV = 1e7 + 10;

struct DSU{
    std::vector<int> lab;
    DSU(int n) : lab(n + 1,-1){

    }
    int findSet(int i){
        return (lab[i] < 0 ? i : lab[i] = findSet(lab[i]));
    }
    bool sameSet(int x,int y){
        return (findSet(x) == findSet(y));
    }
    void merg(int x,int y){
        x = findSet(x);
        y = findSet(y);
        if(x == y){
            return;
        }
        if(-lab[x] > -lab[y]){
            std::swap(x,y);
        }
        lab[y] += lab[x];
        lab[x] = y;
    }
};

bool mark[MaxV + 1];
int n,nxt[MaxV + 1];
std::vector<std::pair<int,int>> edge[MaxV + 1];

void readData(){
    std::cin >> n;
    for(int i = 1;i <= n;++i){
        int v;
        std::cin >> v;
        mark[v] = true;
    }
}
void precompute(){
    int t = -1;
    for(int i = MaxV;i >= 1;--i){
        if(!mark[i]){
            nxt[i] = t;
            continue;
        }
        t = i;
        nxt[i] = i;
        for(int j = i * 2;j <= MaxV;j += i){
            if(mark[j] && i != j){
                edge[0].emplace_back(i,j);
                continue;
            }
            if(nxt[j] != -1){
                j = (nxt[j] / i) * i;
                edge[nxt[j] - j].emplace_back(i,nxt[j]);
            }else{
                break;
            }
        }
    }
}
long long solve(){
    long long ans = 0;
    DSU graph(MaxV);
    for(int i = 0;i <= MaxV;++i){
        for(std::pair<int,int> e : edge[i]){
            if(!graph.sameSet(e.first,e.second)){
                ans += i;
                graph.merg(e.first,e.second);
            }
        }
    }
    return ans;
}
int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);std::cout.tie(nullptr);
    readData();
    precompute();
    std::cout << solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 183 ms 316764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 154 ms 313572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 146 ms 317012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 174 ms 322880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 147 ms 316084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 204 ms 334568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 150 ms 316468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 288 ms 335048 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 290 ms 334524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 185 ms 325204 KB Output isn't correct
2 Halted 0 ms 0 KB -