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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define ll 				long long
#define ld 				long double
#define fastio 			ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x) 		cerr<<#x<<" "<<x<<endl
#define cina(a,n) 		for(ll i = 0;i<n;i++)	cin>>a[i];
#define couta(a,n) 		for(ll i = 0;i<n;i++) cout<<a[i]<<" ";
#define fauto(ar)		for(auto x : ar)	cout<<x<<" ";
#define mod 			1000000007
#define siz 			105
#define f 				first
#define s 				second
#define pb 				push_back
#define endl 			"\n"
#define ci 				cin.ignore()
using namespace std;
ll dp[105][4][2];
ll getans(string num,ll k,ll pos,bool limit)
{
	if(k == 0)
		return 1;
	if(pos == num.length())
		return 0;
	
	if(dp[pos][k][limit] != -1)
	return dp[pos][k][limit];
	
	ll ans = 0,i;
	if(limit)
	{
		if(num[pos] == '0')
		{
			ans += getans(num,k,pos+1,1);
		}
		else
		{
			ans += getans(num,k,pos+1,0);
			for(i = 1;i<num[pos]-'0';i++)
			ans += getans(num,k-1,pos+1,0);
			ans += getans(num,k-1,pos+1,1);
		}	
		
	
	}
	else
	{
		ans += getans(num,k,pos+1,0);
		for(i = 1;i<=9;i++)
		ans += getans(num,k-1,pos+1,0);
	}
	
	return dp[pos][k][limit] = ans;
}
int main()
{
	string num;
	ll k,i,j;
	cin>>num>>k;
	
	memset(dp,-1,sizeof(dp));
	cout<<getans(num,k,0,1);
	
	
	
}