using
System;
public
class
GFG {
public
static
long
FindIntegers(
int
l,
int
r)
{
if
(l == 0) {
return
DigitDP(Convert.ToString(r, 2));
}
l--;
return
DigitDP(Convert.ToString(r, 2))
- DigitDP(Convert.ToString(l, 2));
}
public
static
long
DigitDP(
string
str)
{
long
[, , ] dp =
new
long
[33, 3, 2];
for
(
int
i = 0; i < 33; i++) {
for
(
int
j = 0; j < 3; j++) {
for
(
int
k = 0; k < 2; k++) {
dp[i, j, k] = -1;
}
}
}
return
Solve(str, 0, 2, 1, dp);
}
public
static
long
Solve(
string
str,
int
idx,
int
prev,
int
tight,
long
[, , ] dp)
{
if
(idx >= str.Length) {
return
1L;
}
if
(dp[idx, prev, tight] != -1) {
return
dp[idx, prev, tight];
}
long
ans = 0;
int
upperBound
= (tight == 1) ? (str[idx] -
'0'
) : 1;
for
(
int
dgt = 0; dgt <= upperBound; dgt++) {
int
newTight
= (tight == 0)
? 0
: ((dgt == upperBound) ? 1 : 0);
if
(prev == 1 && dgt == 1) {
continue
;
}
ans += Solve(str, idx + 1, dgt, newTight, dp);
}
dp[idx, prev, tight] = ans;
return
ans;
}
public
static
void
Main(
string
[] args)
{
Console.WriteLine(FindIntegers(0, 3));
Console.WriteLine(FindIntegers(5, 10));
Console.WriteLine(FindIntegers(10, 1000000000));
}
}