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: Runtime Error)


using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;

namespace at_coder
{
#if TEST
    [TestClass]
#endif
    public class abc154_e
    {

#if TEST
        public static void Test_Main()
#else
        public static void Main()
#endif
        {
            string s = Console.ReadLine();
            int k = readint();
            long n = solve(s, k);

            println(n.ToString());
        }

        private static long solve(string s, int k)
        {
            var i = parseS(s);
            return solve(i, k);
        }

        private static long solve(Item i, int k)
        {
            // 全桁 0 の場合を返す
            if (k <= 0)
            {
                return 1;
            }

            // 次の桁を調査する
            long ll = 0;

            int deep = GetDeep(i.Next);

            if (i.A > 0)
            {
                // 0 の場合
                long l2 = C(deep, k);
                for (int j = 0; j < k; j++)
                {
                    l2 *= 9;
                }
                ll += l2;
            }

            // 現在の桁が 0の倍の処理
            if (i.A > 1)
            {
                long deepN = C(deep, k - 1); // 組み合わせの数
                for(int j=0; j < k - 1;j++)
                {
                    deepN = deepN * 9;
                }
                long l3 = deepN * (i.A - 1); // その総数
                ll += l3;
            }

            long l4 = solve(i.Next, k - 1);

            ll += l4;

            return ll;
        }


        static long C(int a, int b)
        {
            long l = 1;
            int d = a;
            for (int i = 0; i < b; i++)
            {
                l = l * d;
                d = d - 1;
            }
            for (int i = 1; i <= b; i++)
            {
                l = l / i;
            }
            return l;
        }

        static int GetDeep(Item i)
        {
            int n = 0;
            while(i != null)
            {
                n++;
                i = i.Next;
            }
            return n;
        }
        

        public static Item parseS(string s)
        {
            Item next = null;
            for(int i=s.Length-1; i >= 0; i--)
            {
                char c = s[i];
                Item a = new Item()
                {
                    A = ((int)c) - ((int)'0'),
                    Next = next
                };

                next = a;
            }

            return next;
        }

        public class Item
        {
            public int A;
            public Item Next;
        }


        private static void println(string s)
        {
            Console.WriteLine(s);
        }

        private static int readint()
        {
            String s = Console.ReadLine();
            return int.Parse(s);
        }

        private static int[] ReadIntArray()
        {
            string[] ss = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int[] ar = new int[ss.Length];
            for (int i = 0; i < ss.Length; i++)
            {
                ar[i] = int.Parse(ss[i]);
            }
            return ar;
        }

#if TEST
#else
    }
}
#endif