using
System;
using
System.Collections.Generic;
public
class
Program
{
static
long
[,,,] dp =
new
long
[20, 2, 2, 2];
static
long
f(
string
nums,
int
idx,
int
even,
int
leadingZero,
int
tight)
{
if
(idx == 0)
{
return
1;
}
if
(dp[idx, even, leadingZero, tight] != -1)
{
return
dp[idx, even, leadingZero, tight];
}
long
cnt = 0;
if
(even == 0)
{
if
(leadingZero == 1)
{
cnt += f(nums, idx - 1, 0, 1, 0);
}
List<
int
> digs =
new
List<
int
> { 1, 3, 5, 7, 9 };
int
ub = (tight == 1) ? (nums[nums.Length - idx] -
'0'
) : 9;
foreach
(
var
i
in
digs)
{
if
(i <= ub)
{
cnt += f(nums, idx - 1, 1, 0, tight & (ub == i ? 1 : 0));
}
}
}
else
{
List<
int
> digs =
new
List<
int
> { 0, 2, 4, 6, 8 };
int
ub = (tight == 1) ? (nums[nums.Length - idx] -
'0'
) : 9;
foreach
(
var
i
in
digs)
{
if
(i <= ub)
{
cnt += f(nums, idx - 1, 0, 0, tight & (i == ub ? 1 : 0));
}
}
}
return
dp[idx, even, leadingZero, tight] = cnt;
}
public
static
void
Main(
string
[] args)
{
long
L = 128, R = 162;
L--;
string
l = L.ToString();
string
r = R.ToString();
for
(
int
i = 0; i < 20; i++)
{
for
(
int
j = 0; j < 2; j++)
{
for
(
int
k = 0; k < 2; k++)
{
for
(
int
m = 0; m < 2; m++)
{
dp[i, j, k, m] = -1;
}
}
}
}
long
ans1 = f(l, l.Length, 0, 1, 1);
for
(
int
i = 0; i < 20; i++)
{
for
(
int
j = 0; j < 2; j++)
{
for
(
int
k = 0; k < 2; k++)
{
for
(
int
m = 0; m < 2; m++)
{
dp[i, j, k, m] = -1;
}
}
}
}
long
ans2 = f(r, r.Length, 0, 1, 1);
Console.WriteLine(ans2 - ans1);
}
}