<?php
function
count_num(
$pos
,
$rem
,
$tight
,
$num
)
{
if
(
$pos
== sizeof(
$num
))
{
if
(
$rem
== 0)
return
1;
return
0;
}
if
(
$GLOBALS
[
'dp'
][
$pos
][
$rem
][
$tight
] != -1)
return
$GLOBALS
[
'dp'
][
$pos
][
$rem
][
$tight
];
if
(
$pos
% 2)
{
if
(
$tight
== 0 &&
$GLOBALS
[
'd'
] >
$num
[
$pos
])
return
0;
$currTight
=
$tight
;
if
(
$GLOBALS
[
'd'
] <
$num
[
$pos
])
$currTight
= 1;
$res
= count_num(
$pos
+ 1, (10 *
$rem
+
$GLOBALS
[
'd'
]) %
$GLOBALS
[
'm'
],
$currTight
,
$num
);
return
$dp
[
$pos
][
$rem
][
$tight
] =
$res
;
}
$ans
= 0;
$limit
= (
$tight
? 9 :
$num
[
$pos
]);
for
(
$dig
= 0;
$dig
<=
$limit
;
$dig
++)
{
if
(
$dig
==
$GLOBALS
[
'd'
])
continue
;
$currTight
=
$tight
;
if
(
$dig
<
$num
[
$pos
])
$currTight
= 1;
$ans
+= count_num(
$pos
+ 1, (10 *
$rem
+
$dig
) %
$GLOBALS
[
'm'
],
$currTight
,
$num
);
}
return
$dp
[
$pos
][
$rem
][
$tight
] =
$ans
;
}
function
solve(
$x
)
{
$num
=
array
() ;
while
(
$x
)
{
array_push
(
$num
,
$x
% 10);
$x
=
floor
(
$x
/ 10);
}
$num
=
array_reverse
(
$num
) ;
for
(
$i
= 0 ;
$i
<
$GLOBALS
[
'M'
] ;
$i
++)
for
(
$j
= 0;
$j
<
$GLOBALS
[
'M'
];
$j
++)
for
(
$k
= 0;
$k
< 2;
$k
++)
$GLOBALS
[
'dp'
][
$i
][
$j
][
$k
] = -1;
return
count_num(0, 0, 0,
$num
);
}
$GLOBALS
[
'M'
] = 20;
$GLOBALS
[
'dp'
] =
array
(
array
(
array
()));
$L
= 10;
$R
= 99;
$GLOBALS
[
'd'
] = 8 ;
$GLOBALS
[
'm'
] = 2;
echo
solve(
$R
) - solve(
$L
) ;
?>