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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder
{

    class Program
    {
        static void Main()
        {
            Solve();

            Console.ReadKey();
        }

        static void Solve()
        {
            var N = GetStr();
            var K = GetInt();
            var C = N.Length;

            var dp = new int[C][][];
            for(int i = 0; i < C; i++)
            {
                dp[i] = new int[2][];
                for(int j = 0; j < 2; j++)
                {
                    dp[i][j] = Enumerable.Repeat(0, K + 1).ToArray();
                }
            }
            dp[0][0][0] = 1;
            dp[0][0][1] = int.Parse(N[0].ToString()) - 1;
            dp[0][1][0] = 0;
            dp[0][1][1] = 1;

            var ans0 = Rec(N, dp, C - 1, 0, K);
            //var ans1 = Rec(N, dp, C - 1, 1, K);
            var ans = ans0 + 1;

            Console.WriteLine(ans);
        }

        static int Rec(string N,int[][][] dp, int i, int j,int k)
        {
            if (i == 0)
            {
                return dp[i][j][k];
            }

            if (j == 0 && k == 0)
            {
                return Rec(N,dp, i - 1, 0, 0);
            }else if (j == 0)//&&k==k
            {
                if (N[i] == '0')
                {
                    return Rec(N, dp, i - 1, 0, k - 1) * 9 + Rec(N, dp, i - 1, 0, k);
                }
                else
                {
                    return Rec(N, dp, i - 1, 0, k - 1) * 9 + Rec(N, dp, i - 1, 0, k) + Rec(N, dp, i - 1, 1, k - 1) * (int.Parse(N[i].ToString()) - 1) + Rec(N, dp, i - 1, 1, k);
                }
            }else if(k == 0) //&& j==1
            {
                return 0;
            }
            else //k==k && j==1
            {
                if (N[i] != '0')
                {
                    return Rec(N, dp, i - 1, 1, k - 1) * (int.Parse(N[i].ToString()) - 1);
                }
                else
                {
                    return Rec(N, dp, i - 1, 1, k);
                }
            }
        }


        static long Conbination(long n, long m)
        {
            if (m == 0) return 1;
            if (n == 0) return 0;
            return n * Conbination(n - 1, m - 1) / m;
        }


        static public string GetStr() { return Console.ReadLine(); }
        static public int GetInt() { return int.Parse(Console.ReadLine()); }
        static public long GetLong() { return long.Parse(Console.ReadLine()); }
        static public string[] GetStrArray() { return Console.ReadLine().Split(' '); }
        static public int[] GetIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
        static public long[] GetLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
        static public List<double> GetDoubleList() { return Console.ReadLine().Split(' ').Select(double.Parse).ToList(); }
        static public void WriteObjects<T>(IEnumerable<T> values) { foreach (var o in values) { Console.Write(o + " "); } }

        static bool eq<T, U>() => typeof(T).Equals(typeof(U));
        static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        static T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                           : eq<T, long>() ? ct<T, long>(long.Parse(s))
                           : eq<T, double>() ? ct<T, double>(double.Parse(s))
                           : eq<T, char>() ? ct<T, char>(s[0])
                                             : ct<T, string>(s);
        static void Multi<T>(out T a) => a = cv<T>(GetStr());
        static void Multi<T, U>(out T a, out U b)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        static void Multi<T, U, V>(out T a, out U b, out V c)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }
    }
}