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 factorial(n)
    local a = 1
    for i=1, n do
        a = a * i
    end
    return a
end

local function nCr(n, r)
    if r > n then
        return 0
    end
    if r > n/2 then
        r = n - r
    end
    local a = 1
    for i=n, n-r+1, -1 do
        a = a * i
    end
    return a // factorial(r)
end

local function normal(d, k)
    return nCr(d, k) * math.floor(9^k)
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 function rec(x, remain)
    if remain == 0 then
        return 1
    end
    for i=x, D do
        if T[i] ~= 0 then
            local ans1 = normal(D-i, remain)
            local ans2 = (T[i] - 1) * normal(D-i, remain-1)
            local ans3 = 1 * rec(i+1, remain-1)
            return ans1+ans2+ans3
        end
    end
    return 0
end

local ans = rec(1, K)
print(ans)