forked from Michael-Calce/GeeksForGeeks-Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26 December Akku and Binary Numbers
61 lines (48 loc) · 1.41 KB
/
26 December Akku and Binary Numbers
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
class Solution{
private:
long long cnts[3][64];
public:
void precompute()
{
// build searching table
cnts[0][63] = cnts[1][63] = cnts[2][63] = 0ll;
// Code Here
for (int i = 0; i < 63; i++) {
cnts[0][i] = i + 1;
if (i < 2) {
cnts[1][i] = 0;
} else {
cnts[1][i] = i - 1 + cnts[1][i-1];
}
if (i < 3) {
cnts[2][i] = 0;
} else {
cnts[2][i] = cnts[1][i-1] + cnts[2][i-1];
}
}
}
long long solve(long long l, long long r){
// Code Here
long long res = 0;
if (l < 1 || r < l || r > 1000000000000000000) {
return -1;
}
long long l_cnt = 0, r_cnt = 0;
// [l, r] ~ (l-1, r]
l--;
// searching all possible 3bits numbers from table
for (int i = 2; i >= 0; i--) {
if (l > 0) {
int cl = 63 - __builtin_clzll(l);
l_cnt += cnts[i][cl];
l ^= 1ll << cl;
}
if (r > 0) {
int cr = 63 - __builtin_clzll(r);
r_cnt += cnts[i][cr];
r ^= 1ll << cr;
}
}
return (r_cnt - l_cnt);
}
};