#include <bits/stdc++.h>
#define task "P"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
int n, m, q, rt[N], rk[N], pa[N][17], dep[N], mn[N][17];
vii g[N];
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
void dfs(int u, int p)
{
pa[u][0] = p;
rep(k, 1, 16) pa[u][k] = pa[pa[u][k - 1]][k - 1], mn[u][k] = min(mn[u][k - 1], mn[pa[u][k - 1]][k - 1]);
dep[u] = dep[p] + 1;
for (ii v : g[u]) if (v.F != p)
{
mn[v.F][0] = v.S;
dfs(v.F, u);
}
}
int query(int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
int mnn = 1e9;
Rep(k, 16, 0) if (dep[pa[x][k]] >= dep[y]) mnn = min(mnn, mn[x][k]), x = pa[x][k];
if (x == y) return mnn;
Rep(k, 16, 0) if (pa[x][k] != pa[y][k])
{
mnn = min({mnn, mn[x][k], mn[y][k]});
x = pa[x][k], y = pa[y][k];
}
return min({mnn, mn[x][0], mn[y][0]});
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> m >> q;
int cnt = 0;
rep(i, 1, n) rt[i] = i;
Rep(i, m, 1)
{
for (int j = 2; j * i <= n; j++)
{
int x = frt(i), y = frt(j * i);
if (x != y)
{
cnt++;
if (rk[x] < rk[y]) swap(x, y);
if (rk[x] == rk[y]) rk[x]++;
rt[y] = x;
g[i].eb(j * i, i);
g[j * i].eb(i, i);
}
if (cnt == n - 1) break;
}
if (cnt == n - 1) break;
}
dfs(1, 0);
while (q--)
{
int u, v;
cin >> u >> v;
cout << m - query(u, v) + 1 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
3704 KB |
Output is correct |
2 |
Correct |
26 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
4224 KB |
Output is correct |
2 |
Correct |
41 ms |
3964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
7424 KB |
Output is correct |
2 |
Correct |
35 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
9208 KB |
Output is correct |
2 |
Correct |
48 ms |
10108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
12024 KB |
Output is correct |
2 |
Correct |
61 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
15384 KB |
Output is correct |
2 |
Correct |
104 ms |
16632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
18168 KB |
Output is correct |
2 |
Correct |
134 ms |
20508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
22392 KB |
Output is correct |
2 |
Correct |
154 ms |
22520 KB |
Output is correct |