Submission #954590

# Submission time Handle Problem Language Result Execution time Memory
954590 2024-03-28T07:51:11 Z HuyAT Sirni (COCI17_sirni) C++14
0 / 140
299 ms 336980 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){
        nxt[i] = t;
        if(!mark[i]){
            continue;
        }
        for(int j = i;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;
            }
        }
        t = i;
    }
}
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 Correct 191 ms 316672 KB Output is correct
2 Incorrect 168 ms 318152 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 140 ms 313684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 145 ms 317012 KB Output is correct
2 Correct 146 ms 314640 KB Output is correct
3 Incorrect 143 ms 317008 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 167 ms 324388 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 145 ms 316112 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 201 ms 335820 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 145 ms 316888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 299 ms 336980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 293 ms 336472 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 176 ms 325516 KB Output isn't correct
2 Halted 0 ms 0 KB -