#include<bits/stdc++.h>
using namespace std;
const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) a = b; else return 0; return 1;
}
const int MAX = 100010;
int N, M, Q;
int h[MAX];
int lab[MAX];
int pp[17][MAX];
int dp[17][MAX];
vector<pair<int, int>> adj[MAX];
vector<pair<int, int>> edges[MAX];
int findSet(int u) {
return lab[u] < 0 ? u : lab[u] = findSet(lab[u]);
}
bool unionSet(int u, int v) {
if ((u = findSet(u)) == (v = findSet(v))) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
void dfs(int u, int p) {
for (auto &[v, w]: adj[u]) if (v != p) {
h[v] = h[u] + 1;
pp[0][v] = u;
dp[0][v] = w;
for (int j = 1; j < 17; ++j) {
dp[j][v] = min(dp[j - 1][v], dp[j - 1][pp[j - 1][v]]);
pp[j][v] = pp[j - 1][pp[j - 1][v]];
}
dfs(v, u);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
int res = M;
for (int j = 0; (1 << j) <= k; ++j) if (k >> j & 1) {
minimize(res, dp[j][u]);
u = pp[j][u];
}
if (u == v) return res;
for (int j = 16; j >= 0; --j) if (pp[j][u] != pp[j][v]) {
minimize(res, dp[j][u]);
minimize(res, dp[j][v]);
u = pp[j][u];
v = pp[j][v];
}
minimize(res, dp[0][u]);
minimize(res, dp[0][v]);
return res;
}
void process(void) {
cin >> N >> M >> Q;
for (int i = M; i >= 1; --i) {
for (int j = i << 1; j <= N; j += i)
edges[i].emplace_back(i, j);
}
memset(lab, -1, sizeof lab);
for (int i = M; i >= 1; --i) {
for (auto &[u, v]: edges[i]) {
if (unionSet(u, v)) {
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
}
}
dfs(1, -1);
while (Q--) {
int A, B; cin >> A >> B;
cout << M - lca(A, B) + 1 << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int nTest = 1; if (multiTest) cin >> nTest;
while (nTest--) {
process();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
pictionary.cpp: In function 'int main()':
pictionary.cpp:104:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pictionary.cpp:105:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |