# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
954597 |
2024-03-28T08:00:47 Z |
HuyAT |
Sirni (COCI17_sirni) |
C++14 |
|
4304 ms |
648224 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;
}
if(t != -1){
edge[t % i].emplace_back(i,t);
}
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 |
Correct |
195 ms |
316752 KB |
Output is correct |
2 |
Correct |
157 ms |
318032 KB |
Output is correct |
3 |
Correct |
148 ms |
317020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
154 ms |
313680 KB |
Output is correct |
2 |
Correct |
141 ms |
314964 KB |
Output is correct |
3 |
Correct |
146 ms |
317368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
144 ms |
316996 KB |
Output is correct |
2 |
Correct |
144 ms |
314680 KB |
Output is correct |
3 |
Correct |
144 ms |
317008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
323592 KB |
Output is correct |
2 |
Correct |
241 ms |
351132 KB |
Output is correct |
3 |
Correct |
179 ms |
328892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
316240 KB |
Output is correct |
2 |
Correct |
209 ms |
338056 KB |
Output is correct |
3 |
Correct |
180 ms |
324236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
335056 KB |
Output is correct |
2 |
Correct |
260 ms |
359952 KB |
Output is correct |
3 |
Correct |
185 ms |
327804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
316756 KB |
Output is correct |
2 |
Correct |
263 ms |
362712 KB |
Output is correct |
3 |
Correct |
192 ms |
327652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
283 ms |
335696 KB |
Output is correct |
2 |
Correct |
1978 ms |
526468 KB |
Output is correct |
3 |
Correct |
315 ms |
339148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
292 ms |
335660 KB |
Output is correct |
2 |
Correct |
3893 ms |
642864 KB |
Output is correct |
3 |
Correct |
364 ms |
343420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
325564 KB |
Output is correct |
2 |
Correct |
4304 ms |
648224 KB |
Output is correct |
3 |
Correct |
184 ms |
329212 KB |
Output is correct |