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: Wrong Answer)

#pragma GCC optimize("O4")
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define MOD (ll)(1e9+7)
#define INF (1e17)
#define int ll
#define EPS (1e-6)
#define DB(x) #x<<" "<<x<<" "
using namespace std;
using ll=long long;
using pii=pair<int,int>;

int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }
int lcm(int a,int b) { return a/gcd(a,b)*b; }
int fpow(int b,int p){
	b%=MOD;
	int ans=1;
	while(p>0){
		if(p&1) ans=(ans*b)%MOD;
		p/=2;
		b=(b*b)%MOD;
	}
	return ans;
}

int dp[110][4][2];
//pos,k,== or <

int32_t main() {
#ifdef LOCAL_TEST
	freopen("D:\\input.in","r",stdin);
	freopen("D:\\output.out","w",stdout);
	freopen("D:\\error.out","w",stderr);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);

	//code...
	string n; cin>>n;
	int k; cin>>k;
	memset(dp,0,sizeof(dp));
	dp[0][0][0]=1;
	dp[0][0][1]=0;
	dp[0][1][0]=n[0]-'0'-1;
	dp[0][1][1]=1;
	For(i,1,sz(n)-1){
		dp[i][0][0]=dp[i-1][0][0];
		if(n[i]!='0') dp[i][0][0]+=dp[i-1][0][1];
		dp[i][0][1]=0;
		if(n[i]=='0') dp[i][0][1]=dp[i-1][0][1];
		For(j,1,k){
			dp[i][j][0]=dp[i-1][j-1][0]*9+dp[i-1][j-1][1]*(n[i]-'0'-1)+dp[i-1][j][0];
			if(n[i]!='0') dp[i][j][0]+=dp[i-1][j][1];
			if(n[i]=='0') dp[i][j][1]=dp[i-1][j][1];
			else dp[i][j][1]=dp[i-1][j-1][1];
		}
	}
	cout<<dp[sz(n)-1][k][0]+dp[sz(n)-1][k][1]<<"\n";
	//For(i,0,sz(n)-1) For(j,0,k)
	//	cerr<<"("<<dp[i][j][0]<<","<<dp[i][j][1]<<")"<<" \n"[j==k];
	return 0;
}