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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace _200523
{
    public class Solve
    {
        public static void Main(string[] args)
        {
            (new Solve())._E();
        }

        string n;
        int len;
        int k;
        long ans = 0;
        void _E()
        {
            n = Ex.Read;
            len = n.Length;
            k = Ex.ReadInt;

            dfs(0, 0, 1, true);

            ans.Cw();
        }

        void dfs(int i, int now_k, long cou, bool b)
        {
            if (now_k > k)
            {
                return;
            }
            if (i >= len)
            {
                if (now_k == k)
                {
                    ans += cou;
                }
                return;
            }

            // 0
            dfs(i + 1, now_k, cou, b && n[i] == '0');

            // 1-9
            if (b)
            {

                int max = n[i] - '0';
                if (max - 1 > 0)
                    dfs(i + 1, now_k + 1, cou * (max - 1), false);
                if (max > 0)
                    dfs(i + 1, now_k + 1, cou, true);
            }
            else
            {
                dfs(i + 1, now_k + 1, cou * 9, false);
            }
        }
    }
    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }


        static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
    }
}