From aa7720bdc6f8ea01978302bef05c7388e2d603d0 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 26 Dec 2022 03:30:06 +0530 Subject: [PATCH] Create 26 December Akku and Binary Numbers 26 December Akku and Binary Numbers --- 26 December Akku and Binary Numbers | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 26 December Akku and Binary Numbers diff --git a/26 December Akku and Binary Numbers b/26 December Akku and Binary Numbers new file mode 100644 index 00000000..03cf7d12 --- /dev/null +++ b/26 December Akku and Binary Numbers @@ -0,0 +1,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); + } + +};