It is dynamic programming on the digits of a number. It involves problems like this one Codeforces problem 489C.

Also, this might be useful : http://stackoverflow.com/questions/22394257/how-to-count-integers-between-large-a-and-b-with-a-certain-property/22394258#22394258