-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcoci15c4p4.cpp
69 lines (69 loc) · 1.59 KB
/
coci15c4p4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Ivan Carvalho
// Solution to https://dmoj.ca/problem/coci15c4p4
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, K, Q;
ll get_pow(ll pot) {
ll resp = 1;
for (ll i = 1; i <= pot; i++) resp *= K;
return resp;
}
ll get_level(ll x) {
ll pot = 1, nivel = 0;
while (x > 0) {
// printf("%lld\n",x);
x -= pot;
pot *= K;
nivel++;
}
return nivel;
}
ll get_first_level(ll nivel) {
if (nivel == 0) return 1;
ll ac = 0;
ll pot = 1;
for (ll i = 1; i < nivel; i++) {
ac += pot;
pot *= K;
}
return ac + 1;
}
ll get_parent(ll x) {
if (x == 1) return 1;
ll nivel = get_level(x);
ll primeiro = get_first_level(nivel - 1);
ll ultimo = get_first_level(nivel) - 1;
ll ini = primeiro, fim = ultimo, meio;
while (ini <= fim) {
meio = (ini + fim) / 2;
ll delta = meio - primeiro;
ll esimo1 = ultimo + 1 + delta * K;
ll esimok = esimo1 + K - 1;
if (esimo1 <= x && x <= esimok) {
return meio;
}
if (x < esimo1) {
fim = meio - 1;
} else
ini = meio + 1;
}
}
ll LCA(ll u, ll v) {
while (u != v) {
if (get_level(u) < get_level(v)) swap(u, v);
// printf("U %lld P %lld\n",u,get_parent(u));
u = get_parent(u);
}
return u;
}
int main() {
scanf("%lld %lld %lld", &N, &K, &Q);
while (Q--) {
ll u, v;
scanf("%lld %lld", &u, &v);
printf("%lld\n",
get_level(u) + get_level(v) - 2 * get_level(LCA(u, v)));
}
return 0;
}