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 JavaScript (node.js v5.12) (judge verdict: Runtime Error)

const factorial=(num=>num<=1?1:num*factorial(num-1));

//パターン取得
const getPattern=((n,k,keta,i,xnum,pflag)=>{
  if(i>=keta){return 1;}
  let num1=0;
  let num2=0;
  let num=0;

  //上位がぴったり
  if(pflag){
    //対象が0じゃない
    if(n[i]!==0){
      //0が存在する
      if(n-i!=k-xnum){
        //xが存在する
        if(k-xnum>0){
          //先頭が0になるパターン
          const pNum=factorial(keta-i-1)/(factorial(k-xnum)*factorial(keta-i-1-k+xnum));

          //総数
          num1=(k-xnum)*9*pNum;
        }
        else{
          num1=1;
        }
      }

      //xが存在する
      if(k-xnum>0){
        if(n[i]>1){
          //ぴったりじゃない
          num2=(n[i]-1)*getPattern(n,k,keta,i+1,xnum+1,false);
        }
        //ぴったり
        num2=getPattern(n,k,keta,i+1,xnum+1,true);
      }

      num=num1+num2;
    }

    //対象が0
    else{
      num=getPattern(n,k,keta,i+1,xnum,true);
    }
  }
  //上位がぴったりじゃ無い
  else{
    const pNum=factorial(keta-i)/(factorial(k-xnum)*factorial(keta-i-k+xnum));
    num=(k-xnum)*9*pNum;
  }

  return num;
});

function Main(input){
  var inputs=input.split("\n");
  const n=inputs[0];
  const k=inputs[1];
  const keta=inputs[0].length;
  
  const num=getPattern(n,k,keta,0,0,true);

  console.log(num);
}
Main(require('fs').readFileSync('/dev/stdin','utf8'));