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# (.NET Core 3.1.201) (judge verdict: Runtime Error)

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

namespace AtCoders {
	class Program {
		static void Main(string[] args) {

			var inputArray = retreiveStringArray();
			var inputCount = 1;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			var N = inputArray[0].Select(x=>int.Parse(x.ToString())).ToArray();
			var K = retreiveLongArray()[0];


			long ans = 0;
			//N桁目が0の場合の数
			ans += combination(N.Length - 1, K) * (long)Math.Pow(9, K);


			//N桁目が1以上nより小さいとき
			ans += combination(N.Length - 1, K-1) * (long)Math.Pow(9, K-1)*(N[0]-1);

			//N桁目がnのとき
			if (K==1) {//K=1ならn000000を足しておしまい
				ans += 1;
				Console.WriteLine(ans);
				return;
			}

			var k = K - 1;
			var i = 1;
			while (k>0 ) {
				if (N[i]==0) {
					i++;
					if (i == N.Length) {
						break;
					}
					continue;
				}
				var n = N[i];
				//N-i桁目が0の場合
				ans += combination(N.Length - (i+1), k) * (long)Math.Pow(9, k);
				//N-i桁目が1以上(n-i)(その桁の数値)より小さいとき
				ans += combination(N.Length - (i + 1), k - 1) * (long)Math.Pow(9, k - 1) * (n - 1);
				if (k==1) {
					ans += 1;
					Console.WriteLine(ans);
					return;
				}
				k--;
				i++;
				if (i==N.Length) {
					break;
				}
			}

			//答えの出力
			Console.WriteLine(ans);

		}

		/// <summary>
		/// nこからrこを選ぶ組み合わせの数
		/// </summary>
		/// <param name="n"></param>
		/// <param name="r"></param>
		/// <returns></returns>
		private static long combination(long n, long r) {

			long bunshi = 1;
			for (int i = 1; i <= r; i++) {
				bunshi = bunshi * (n - r + i);
			}

			long bunbo = 1;
			for (int i = 1; i <= r; i++) {
				bunbo = bunbo * (i);
			}
			//逆元を使って、組み合わせの数を求める	
			return bunshi / bunbo;

		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}


		/// <summary>
		/// 2組みずつの入力データ(数値)からTuppleListを作成する
		/// </summary>
		/// <param name="N"></param>
		/// <returns></returns>
		private static List<Tuple<long, long>> createLongTuppleList(long N) {
			var inpList = new List<Tuple<long, long>>();

			for (int i = 0; i < N; i++) {
				var inp = retreiveLongArray();
				var item1 = inp[0];
				var item2 = inp[1];
				inpList.Add(new Tuple<long, long>(item1, item2));
			}
			return inpList;
		}
	}
}