using
System;
using
System.Numerics;
class
GFG
{
public
static
string
toBinaryString(
int
x)
{
char
[] bits =
new
char
[32];
int
i = 0;
while
(x != 0)
{
bits[i++] = (x & 1) == 1 ?
'1'
:
'0'
;
x >>= 1;
}
Array.Reverse(bits, 0, i);
return
new
string
(bits);
}
static
int
[] preCalculate(
int
max,
string
pattern)
{
int
[]arr =
new
int
[max + 1];
if
(pattern ==
"0"
)
arr[0] = 1;
else
arr[0] = 0;
for
(
int
i = 1; i <= max; i++)
{
if
(toBinaryString(i).Contains(pattern))
{
arr[i] = 1 + arr[i - 1];
}
else
{
arr[i] = arr[i - 1];
}
}
return
arr;
}
static
void
performQueries(
int
[,]queries,
int
q,
string
pattern)
{
int
max =
int
.MinValue;
for
(
int
i = 0; i < q; i++)
max = Math.Max(max, queries[i, 1]);
int
[]res = preCalculate(max, pattern);
for
(
int
i = 0; i < q; i++)
{
int
l = queries[i, 0];
int
r = queries[i, 1];
if
(l == 0)
Console.WriteLine(res[r]);
else
Console.WriteLine(res[r] - res[l - 1]);
}
}
public
static
void
Main(
string
[]args)
{
int
[,]queries = { { 2, 10 }, { 8, 120 } };
int
q = queries.GetLength(0) ;
string
pattern =
"101"
;
performQueries(queries, q, pattern);
}
}