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.Text.RegularExpressions;
using System.Collections.Generic;
using static program.Input;
using static program.MyMath;
using static System.Console;
using static program.Const;

namespace program
{
  class Program
  {
    static void Main()
    {
      var n = ScanStr().Select(_=>int.Parse($"{_}")).ToArray();
      var k = ScanInt();
      var l = n.Length;
      var dpF = new int[l][];
      var dpT = new int[l][];
      for(int i=0; i<l; i++){
        dpF[i] = new int[k+1];
        dpT[i] = new int[k+1];
      }
      dpF[0][0] = 1;
      dpF[0][1] = n[0]-1;
      dpT[0][1] = 1;
      for(int i=1; i<l; i++){
        dpF[i][0] = dpF[i-1][0] + (n[i]!=0 ? dpT[i-1][0] : 0);
        for(int j=1; j<=k; j++){
          dpF[i][j] = dpF[i-1][j-1]*9 + dpF[i-1][j]
                    + dpT[i-1][j-1]*Math.Max(0,n[i]-1)
                    + dpT[i-1][j]  *(n[i]!=0 ? 1 : 0);
          dpT[i][j] = (n[i]==0 ? dpT[i-1][j] : dpT[i-1][j-1]);
        }
      }
      WriteLine(dpF[l-1][k] + dpT[l-1][k]);
    }
  }

  static class Const
  {
    public static string alp = "abcdefghijklmnopqrstuvwxyz";
    public static string Alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static long MOD = 1000000007;
    public static int INF = int.MaxValue;
    public static long INFL = long.MaxValue;
  }
  static class Input
  {
    static public int ScanInt() { return int.Parse(Console.ReadLine()); }
    static public uint ScanUint() { return uint.Parse(Console.ReadLine()); }
    static public double ScanDouble() { return double.Parse(Console.ReadLine()); }
    static public long ScanLong() { return long.Parse(Console.ReadLine()); }
    static public ulong ScanUlong() { return ulong.Parse(Console.ReadLine()); }
    static public int[] ScanIntArray() { return Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray(); }
    static public uint[] ScanUintArray() { return Console.ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); }
    static public double[] ScanDoubleArray() { return Console.ReadLine().Split().Select(x => double.Parse(x)).ToArray(); }
    static public long[] ScanLongArray() { return Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray(); }
    static public ulong[] ScanUlongArray() { return Console.ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); }

    static public string ScanStr() { return Console.ReadLine(); }
    static public string[] ScanStrArray() { return ScanStr().Split(); }
    static public string RegaxReplace(this string arg1, string pattern, string replacement) { return Regex.Replace(arg1, pattern, replacement); }
    static public string Join<T>(this IEnumerable<T> source, string separator) { return string.Join(separator, source); }
  }

  static class MyMath
  {
    static public int Floor2(this int a)
    {
      int N = 1;
      while (N < a) N *= 2;
      return N / 2;
    }
    static public long Floor2(this long a)
    {
      long N = 1;
      while (N < a) N *= 2;
      return N / 2;
    }
    static public int Ceiling2(this int a)
    {
      int N = 1;
      while (N < a) N *= 2;
      return N;
    }
    static public long Ceiling2(this long a)
    {
      long N = 1;
      while (N < a) N *= 2;
      return N;
    }
    static public long[] CumSum(this long[] a)
    {
      var res = new long[a.Length];
      long s = 0;
      for (int i = 0; i < a.Length; i++) res[i] = (s += a[i]);
      return res;
    }
    static public IEnumerable<int> PrimeFactors(this int n)
    {
      int tmp = n;
      for (int i = 2; i * i <= n; i++)
      {
        while (tmp % i == 0)
        {
          tmp /= i;
          yield return i;
        }
      }
      if (tmp != 1) yield return tmp;
    }
    static public IEnumerable<long> PrimeFactors(this long n)
    {
      var tmp = n;
      for (long i = 2; i * i <= n; i++)
      {
        while (tmp % i == 0)
        {
          tmp /= i;
          yield return i;
        }
      }
      if (tmp != 1) yield return tmp;
    }
    static public int Digit(this int n, int d) { if (d < 0) return 0; return (n % (int)Math.Pow(10, d + 1)) / (int)Math.Pow(10, d); }
    static public int Length(this int n) { if (n < 0) return 0; return (int)Math.Log10(n) + 1; }
    static public int Top(this int n) { return n.Digit(n.Length() - 1); }
    public static bool isPrime(this int p)
    {
      if (p <= 1) return false;
      for (int i = 2; i * i <= p; i++)
        if (p % i == 0) return false;
      return true;
    }
    public static bool isPrime(this long p)
    {
      if (p <= 1) return false;
      for (long i = 2; i * i <= p; i++)
        if (p % i == 0) return false;
      return true;
    }
    public static int GCD(int a, int b)
    {
      Func<int, int, int> gcd = null;
      gcd = (x, y) => y == 0 ? x : gcd(y, x % y);
      return a > b ? gcd(a, b) : gcd(b, a);
    }
    public static long GCD(long a, long b)
    {
      Func<long, long, long> gcd = null;
      gcd = (x, y) => y == 0 ? x : gcd(y, x % y);
      return a > b ? gcd(a, b) : gcd(b, a);
    }
    public static ulong extGCD(ulong a, ulong b, ref ulong x, ref ulong y)
    {
      if (b == 0)
      {
        x = 1;
        y = 0;
        return a;
      }
      var d = extGCD(b, a % b, ref y, ref x);
      y -= a / b * x;
      return d;
    }
    public static int LCM(int a, int b) { return a * b / GCD(a, b); }
    public static long LCM(long a, long b) { return a * b / GCD(a, b); }
    public static long Inv(this long a, long m = 1000000007)
    {
      long b = m, u = 1, v = 0;
      while (b != 0)
      {
        long t = a / b;
        a -= t * b;
        swap(ref a, ref b);
        u -= t * v;
        swap(ref u, ref v);
      }
      u %= m;
      if (u < 0) u += m;
      return u;
    }
    private static void swap(ref long a, ref long b)
    {
      var t = a;
      a = b;
      b = t;
    }
  }


}