Problem:
Score : 500 points

Problem Statement

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.

Constraints

1 \leq N < 10^{100}

1 \leq K \leq 3

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the count.

Sample Input 1

100
1

Sample Output 1

19

The following 19 integers satisfy the condition:

1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100

Sample Input 2

25
2

Sample Output 2

14

The following 14 integers satisfy the condition:

11,12,13,14,15,16,17,18,19,21,22,23,24,25

Sample Input 3

314159
2

Sample Output 3

937

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

Sample Output 4

117879300

Solution in Lua (5.3.2) (judge verdict: Accepted)

local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.T = function(T) local t={} for i=1,T do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
local function create_ndim_array(n, last_metatbl)
    local metatables = {}
    metatables[1] = last_metatbl
    for i=2,n do
        metatables[i] = {__index = function(parent, k)
            local child = setmetatable({}, metatables[i-1])
            rawset(parent, k, child)
            return child
        end}
    end
    return setmetatable({}, metatables[n])
end
-- E
local S = read.l():totable()
local T = {}
for i=1,#S do
    T[i] = tonumber(S[i])
end
local D = #T
local K = read.n()

local tbl = create_ndim_array(3, {__index = function() return 0 end})
tbl[0][true][0] = 1
for i=0,D-1 do
    for _, m in ipairs {false, true} do
        for k=0,K do
            if tbl[i][m][k] == 0 then
                goto continue_k
            end
            local ni = i+1
            local nd = T[ni]
            for d=0,9 do
                local nk = k
                local nm
                if m == false then
                    nm = false
                else
                    if d > nd then
                        break
                    elseif d == nd then
                        nm = true
                    else
                        nm = false
                    end
                end
                if d ~= 0 then
                    nk = nk + 1
                end
                if nk > K then
                    goto continue_d
                end
                tbl[ni][nm][nk] = tbl[ni][nm][nk] + tbl[i][m][k]
                ::continue_d::
            end
            ::continue_k::
        end
    end
end
local ans = tbl[D][false][K] + tbl[D][true][K]
print(ans)