-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCF2075_D_Equalization.cpp
More file actions
65 lines (56 loc) · 1.97 KB
/
Copy pathCF2075_D_Equalization.cpp
File metadata and controls
65 lines (56 loc) · 1.97 KB
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
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
// vector<vector<vector<ll>>> memo;
ll memo[70][70][70][64];
ll dp(ll cur_lcp, ll a_left, ll b_left, ll next) {
if (memo[cur_lcp][a_left][b_left][next] != -1) return memo[cur_lcp][a_left][b_left][next];
if (a_left == b_left && a_left <= cur_lcp) return memo[cur_lcp][a_left][b_left][next] = 0; // we can just skip the rest
ll ans = 1e18;
if (next + 1 <= 63) {
ans = min(ans, dp(cur_lcp, a_left, b_left, next + 1)); // skip this one
ans = min(ans, dp(cur_lcp, max(0LL, a_left - next), b_left, next + 1) + (1LL << next));
ans = min(ans, dp(cur_lcp, a_left, max(0LL, b_left - next), next + 1) + (1LL << next));
}
return memo[cur_lcp][a_left][b_left][next] = ans;
}
int main() {
int tc;
cin >> tc;
memset(memo, -1, sizeof(memo));
while (tc--) {
ll x, y;
cin >> x >> y;
// we find the longest common prefix of x and y in binary
int xlen, ylen;
if (x == 0)
xlen = 0;
else
xlen = 64 - __builtin_clzll(x);
if (y == 0)
ylen = 0;
else
ylen = 64 - __builtin_clzll(y);
ll lcp = 0;
for (int i = 0; i < min(xlen, ylen); i++) {
bool bitx = (x & (1LL << (xlen - 1 - i))) > 0;
bool bity = (y & (1LL << (ylen - 1 - i))) > 0;
if (bitx == bity)
lcp++;
else
break;
}
// cerr << "lcp: " << lcp << endl;
// cerr << "xlen: " << xlen << " ylen: " << ylen << endl;
// memo.assign(xlen + 2, vector<vector<ll>>(ylen + 2, vector<ll>(max(xlen, ylen) + 5, -1)));
cout << dp(lcp, xlen, ylen, 1) << endl;
}
return 0;
}