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;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }


int main() {
  string N; cin >> N;
  int l = N.size();
  int K; cin >> K;
  string zero; rep(i,l) zero += '0';
  vector<int> nz; rep(i,l) if (N[i]!='0') nz.push_back(i);

  ll result = 0;
  if (K==1) result = (N[0]-'0') + (l-1)*9;
  else if (K==2) {
    repr(i,1,l) {
      repr(in,1,10) {
        string tmp = zero;
        string ln = ""; ln += ('0'+in);
        tmp.replace(0,1,N.substr(0,1)); tmp.replace(i,1,ln);
        if (tmp<=N) result++;
      }
    }
    result += (N[0]-'1') * ((l-1)*9);
    result += (l-1)*(l-2)/2*9*9;
  }
  else if (K==3) {
    repr(i,1,l) repr(j,i+1,l) {
      repr(in,1,10) repr(jn,1,10) {
        string tmp = zero;
        string ln = "", rn = ""; ln += ('0'+in); rn += ('0'+rn);
        tmp.replace(0,1,N.substr(0,1)); tmp.replace(i,1,ln); tmp.replace(j,1,rn);
        if (tmp<=N) result++;
      }
    }
    result += (N[0]-'1') * (l-1)*(l-2)/2*9*9;
    result += (l-1)*(l-2)*(l-3)/6*9*9*9;
  }

  cout << result << endl;
  return 0;
}