#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
if(a>b) swap(a,b);
while(a!=1){
b = b%a;
if(b == 0) return a;
if(a > b) swap(a,b);
}
return 1;
}
struct dsu{
vector<int> pai, rank, tempo;
int n,t;
dsu(int a){
pai.assign(a,-1); rank.assign(a,0); tempo.assign(a,1e6);
n = a;
t=0;
}
int find(int a){
if(pai[a] == -1) return a;
return find(pai[a]);
}
void join(int a, int b){
a = find(a); b = find(b);
if(a == b) return;
if(rank[a] > rank[b]){pai[b] = a; tempo[b] = t;}
else if(rank[b] > rank[a]){pai[a] = b; tempo[a] = t;}
else{pai[b] = a; rank[a]++; tempo[b] = t;}
return;
}
int query(int a, int b){
int res = -1;
while(a != b){
if(tempo[a] < tempo[b]){res = tempo[a]; a = pai[a];}
else {res = tempo[b]; b = pai[b];}
if(a == b)return res;
}
return res;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,q; cin >> n >> m >> q;
dsu v(n);
for(int i=1; i<=m; i++){
v.t++;
for(int a = 1; a<=n; a++) for(int b = a+1; b<=n; b++){
if(gcd(a,b) != (m-i + 1)) continue;
v.join(a-1, b-1);
}
}
while(q--){
int a,b; cin >> a >> b;
a--; b--;
cout << v.query(a,b) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
645 ms |
484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1560 ms |
340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1571 ms |
340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1548 ms |
596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1579 ms |
724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1575 ms |
852 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1549 ms |
1108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1558 ms |
1236 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
1492 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |