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>
using namespace std;
#define endl '\n'
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
const double ex=1e-5;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=200005;
const int mod=1000000007;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
char s[1005];
int n,k;
int dp[105][4][2];
int dfs(int pos,int cnt,int limit)
{
	if(pos==n+1)return cnt==k;//如果恰为k位，回溯1，累加到前面，否则忽视这个情况
	if(cnt>k)return 0;//cnt不是k忽视这个情况
	if(dp[pos][cnt][limit]!=-1)//已经访问过啦
	{
		return dp[pos][cnt][limit];
	}
	int high=limit?s[pos]-'0':9;
	int sum=0;
	for(int i=0;i<=high;i++)
	{
		sum+=dfs(pos+1,cnt+(i!=0),limit&&i==high);//向下一层搜索
	}
	return dp[pos][cnt][limit]=sum;//返回时要记忆化!
	
}
int main()
{
	memset(dp,-1,sizeof(dp));
	cin>>s+1;
	cin>>k;
	n=strlen(s+1);
	cout<<dfs(1,0,1);//保证
}