-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDigitDp.cpp
54 lines (52 loc) · 1.14 KB
/
DigitDp.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
#include <vector>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
LL dp[65][2][3][3][2];
vector<int> bitsOfx(61);
LL solve(int index, int ans, int prev, int pPrev, int flag) {
if (index == -1) {
return ans;
}
LL sol = 0;
if (dp[index][ans][prev][pPrev][flag] != -1)
return dp[index][ans][prev][pPrev][flag];
if (bitsOfx[index])
{
sol += solve(index - 1, (pPrev == 1 && prev == 0 ? 1 : ans), 1, prev, flag); // 1
sol += solve(index - 1, ans, 0, prev, 1); // 0
}
else {
if (flag) {
sol += solve(index - 1, (pPrev == 1 && prev == 0 ? 1 : ans), 1, prev, flag); // 1 0 1
sol += solve(index - 1, ans, 0, prev, 1); // 1 0 0
}
else {
sol += solve(index - 1, ans, 0, prev, 0);
}
}
return dp[index][ans][prev][pPrev][flag] = sol;
}
LL preCompute(LL x) {
for (int i = 0; i <= 60; i++) {
bitsOfx[i] = 0;
if (x & (1LL << i)) {
bitsOfx[i] = 1;
}
}
memset(dp, -1, sizeof(dp));
return solve(60, 0, 2 , 2, 0);
}
LL ans(LL L, LL R) {
return preCompute(R) - preCompute(L - 1);
}
int main() {
int T;
cin >> T;
while (T--) {
LL L, R;
cin >> L >> R;
cout << ans(L, R);
}
}