#include <bits/stdc++.h>
#define PB push_back
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 200100;
vector<int> qr[N];
int pr[N], n, m, q, x[N], y[N], L[N], R[N], mid[N];
int get(int x) { return (pr[x] == x ? x : pr[x] = get(pr[x])); }
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m >> q;
for (int i = 0; i < q; i++){
cin >> x[i] >> y[i];
L[i] = 1; R[i] = m;
}
bool was = 1;
while (was){
was = 0;
for (int i = 0; i < q; i++)
if (L[i] < R[i]){
mid[i] = (L[i] + R[i]) >> 1;
qr[m - mid[i] + 1].PB(i);
was = 1;
}
for (int i = 1; i <= n; i++)
pr[i] = i;
for (int i = m; i > 1; i--){
for (int j = i + i; j <= n; j += i)
pr[get(j)] = get(i);
if (sz(qr[i])) {
for (int nm : qr[i])
if (get(x[nm]) == get(y[nm]))
R[nm] = mid[nm];
else L[nm] = mid[nm] + 1;
qr[i].clear();
}
}
}
for (int i = 0; i < q; i++)
cout << L[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
9464 KB |
Output is correct |
2 |
Correct |
37 ms |
9076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11208 KB |
Output is correct |
2 |
Correct |
50 ms |
10944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
9652 KB |
Output is correct |
2 |
Correct |
79 ms |
9844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
10156 KB |
Output is correct |
2 |
Correct |
74 ms |
9720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
12340 KB |
Output is correct |
2 |
Correct |
133 ms |
10608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
10268 KB |
Output is correct |
2 |
Correct |
208 ms |
14580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
259 ms |
15412 KB |
Output is correct |
2 |
Correct |
250 ms |
15220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
320 ms |
17392 KB |
Output is correct |
2 |
Correct |
284 ms |
16884 KB |
Output is correct |