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)

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using f80 = __float80;
constexpr i64 INF = 1'010'000'000'000'000'017LL;
constexpr i64 MOD = 1'000'000'007LL;
static const int dy[4] = {0,1,0,-1}, dx[4] = {1,0,-1,0};
#define _overload3(_1,_2,_3,name,...) name
#define repi(i,start,end) for(i64 i=(start);i<(end);++i)
#define _rep(i,n) repi(i,0,n)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,start,end) for(i64 i=(start);i>end;--i)
#define pb push_back
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define sz(x) (i64)(x).size()
#define bit(n) (1LL<<(n))
typedef vector<i64> vi;
typedef vector<vi> vvi;
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}

i64 pow_mod(i64 n, i64 k, i64 m){
  i64 r = 1;
  for(;k;k>>=1){
    if(k&1) r=(r*n)%m;
    n=(n*n)%m;
  }
  return r;
}

void Main()
{
  string N;
  cin >> N;
  i64 K;
  cin >> K;

  i64 ans=0;

  if(K==1){
    ans = i64(N[0]-'0')+(sz(N)-1)*9;
  }
  if(K==2){
    ans += 9*9*(sz(N)-1)*(sz(N)-2)/2;
    ans += i64(N[0]-'0'-1)*9*(sz(N)-1);
    rep(i,1,sz(N)){
      if(N[i]=='0') continue;
      ans += i64(N[i]-'0');
      ans += (sz(N)-1-i)*9;
      break;
    }
  }
  if(K==3){
    ans += 9*9*9*(sz(N)-1)*(sz(N)-2)*(sz(N)-3)/(3*2);
    ans += i64(N[0]-'0'-1)*9*9*(sz(N)-1)*(sz(N)-2)/2;
    rep(i,1,sz(N)){
      if(N[i]=='0') continue;
      ans += 9*9*(sz(N)-1-i)*(sz(N)-2-i)/2;
      ans += i64(N[i]-'0'-1)*9*(sz(N)-1-i);
      rep(ii,i+1,sz(N)){
        if(N[i]=='0') continue;
        ans += i64(N[ii]-'0');
        ans += (sz(N)-1-ii)*9;
        break;
      }
      break;
    }
  }

  cout << ans << endl;
}

int main()
{
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}