#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
template <int maxn>
struct Dsu {
int n;
int pai[maxn], w[maxn];
int find(int x)
{
if (x == pai[x]) return x;
return pai[x] = find(pai[x]);
}
bool find(int a, int b)
{
a = find(a), b= find(b);
return a == b;
}
void join(int a, int b)
{
a = find(a), b = find(b);
if (w[a] < w[b]) swap(a, b);
pai[b] = a;
w[a] += w[b];
}
void init(int n_) {
n = n_;
fill(w+1, w+n+1, 1);
iota(pai+1, pai+n+1, 1);
}
};
int const maxn = 101010;
Dsu<maxn> dsu;
int main()
{
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
int q;
cin >> n >> m >> q;
while (q--) {
dsu.init(n);
int a, b;
cin >> a >> b;
bool ok = false;
for (int k = m; k >= 2; k--) {
for (int i = k*2; i <= n; i += k) {
if (!dsu.find(k, i)) dsu.join(k, i);
}
if (dsu.find(a, b)) {
cout << m-k+1 << "\n";
ok = true;
break;
}
}
if (!ok) cout << m << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1046 ms |
908 KB |
Output is correct |
2 |
Correct |
777 ms |
908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1580 ms |
936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1572 ms |
936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1580 ms |
984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1575 ms |
992 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1571 ms |
1136 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1575 ms |
1520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |