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>
#define rep(i,k,n)     for (ll i = k; i < n; ++i)
#define repr(i,k,n)    for (ll i = k; i >= n; --i)
#define repp(p,a)      for (p=a.begin();p!=a.end();++p)
#define all(a)         a.begin(),a.end()
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define reppn(p,a,n)    for (p=a.begin();(p+n)!=a.end();++p)
#define F              first
#define S              second
#define ll             long long
#define hell           1000000007
#define god            LLONG_MAX
#define lb             lower_bound
#define ub             upper_bound
#define pb             push_back
#define ct             continue
#define endl           '\n'
#define sp(n)          setprecision(n)
#define srt(a)         sort(all(a))
#define srtd(a)        sort(a.rbegin(),a.rend())
#define sz(a)          ll((a).size())
#define sum(a)         accumulate(all(a),(ll) 0)
#define pdt(a)         accumulate(all(a),double(1),multiplies<double> ())
#define dotpdt(a,b)    inner_product(all(a),b.begin(),0)
#define mkp(a,b)       make_pair(a,b)
#define REP(x,s)       for (auto x : s)
using namespace std;

ll dp3(ll n)
{
    return 121.5*(n)*(n-1)*(n-2);
}
ll dp2(ll n)
{
    return 40.5*(n)*(n-1);
}
ll dp1(ll n)
{
    return 9*n;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t=1;
  //cin>>t;
  while(t--)
  {
      string sp,s="";
      ll k;
      cin>>sp>>k;
      ll n=sp.size(),k1,k2,k3,c=0,z=0,b=0,flag1=1,flag2=1,flag3=1;
      rep(i,0,n)
      {
          if(sp[i]!='0')
          {
              z++;
          }
      }
      //cout<<z<<endl;
      if(k==3)
      {
          if(z==1)
          {
              if(sp.size()>=4)
              {
                  s+=to_string(((sp[0]-'0')-1));
                  rep(i,0,sp.size()-1)
                  {
                      s+='9';
                      flag1=0;
                  }
              }
              else
              {
                  cout<<0;
                  return 0;
              }
          }
          else if (z==2)
          {
              flag2=0;
              repr(i,n-1,0)
              {
                  if(sp[i]!='0')
                  {
                      b=i;
                      break;
                  }
              }
              //cout<<b<<endl;
              if(b>=(n-2))
              {
                  s+=to_string(((sp[0]-'0')-1));
                  rep(i,0,sp.size()-1)
                  {
                      s+='9';
                  }
              }
              else
              {
                  //cout<<"hi"<<endl;
                  rep(i,0,b)
                  {
                      s+=sp[i];
                  }
                  s+=to_string(((sp[b]-'0')-1));
                  rep(i,b,sp.size()-1)
                  {
                      s+='9';
                  }
              }
          }
      }
      else if(k==2)
      {
          if(z==1)
          {
              flag3=0;
              if(sp.size()>=3)
              {
                  s+=to_string(((sp[0]-'0')-1));
                  rep(i,0,sp.size()-1)
                  {
                      s+='9';
                  }
              }
              else
              {
                  cout<<0;
                  return 0;
              }
          }
      }
      if(flag1*flag2*flag3)
      {
          rep(i,0,n)
          {
              s+=sp[i];
          }
      }
      rep(i,0,n)
      {
          if(s[i]!='0')
          {
              c++;
              if(c==1)
              {
                  k1=i;
              }
              else if(c==2)
              {
                  k2=i;
              }
              else if(c==3)
              {
                  k3=i;
              }
          }
      }
      
      if(k==3)
      {
          ll n1=n-k1,n2=n-k2,n3=n-k3;
          cout<<dp3(n1-1)+(dp2(n1-1)*((s[k1]-'0')-1))+dp2(n2-1)+(dp1(n2-1)*((s[k2]-'0')-1))+( (s[k3]-'0')*n3+(9-(s[k3]-'0'))*(n3-1));
      }
      else if(k==2)
      {
          ll n1=n-k1,n2=n-k2;
          cout<<dp2(n1-1)+dp1(n1-1)*((s[k1]-'0')-1)+(s[k2]-'0')*n2+(9-(s[k2]-'0'))*(n2-1);
      }
      else
      {
          ll n1=n-k1;
          cout<<(s[k1]-'0')*n1+(9-(s[k1]-'0'))*(n1-1);
      }
  }      
 
 
 return 0;
}