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.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;

namespace AtCoder
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static long Mod = (long)1e9 + 7;
        static long N, M, Q;
        static long[] a, b, c, d;
        static void Main(string[] args)
        {
            SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            Out.Flush();
            Read();
        }
        static void Solve()
        {
            var S = ReadLine();
            var N = S.Length;
            var K = Sarray()[0];


            var dp = new long[N, 10, 4, 2];
            var F = 0;
            var T = 1;
            var Ss = S[0] - '0';
            for (var l = 1; l < Ss; ++l)
                dp[0, l, 1, F] = 1;
            dp[0, Ss, 1, T] = 1;
            for (var i = 1; i < N; ++i)
            {
                var Sn = S[i] - '0';
                var Sp = S[i - 1] - '0';

                for (var k = 1; k <= 3; ++k)
                {
                    //0に推移する場合
                    for (var j = 0; j <= 9; ++j)
                        dp[i, 0, k, F] += dp[i - 1, j, k, F];
                    if (0 != Sn)
                        dp[i, 0, k, F] += dp[i - 1, Sp, k, T];

                    //0以外に遷移する場合
                    for (var l = 1; l <= 9; ++l)
                        for (var j = 0; j <= 9; ++j)
                            dp[i, l, k, F] += dp[i - 1, j, k - 1, F];
                    for (var l = 1; l < Sn; ++l)
                        dp[i, l, k, F] += dp[i - 1, Sp, k - 1, T];

                    //張り付き
                    if (0 == Sn)
                        dp[i, Sn, k, T] = dp[i - 1, Sp, k, T];
                    else
                        dp[i, Sn, k, T] = dp[i - 1, Sp, k - 1, T];
                }
                //前全部0から推移
                for (var l = 1; l <= 9; ++l)
                    ++dp[i, l, 1, F];
            }
            var ans = 0L;
            for (var l = 0; l <= 9; ++l)
                for (var t = 0; t <= 1; ++t)
                    ans += dp[N - 1, l, K, t];
            WriteLine(ans);
        }
 
    }
}