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++ (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<cstdio>
// #include<string>
using namespace std;

int comb(int a, int b){
    int x = 1;
    for(int i = 0; i < b; i++){
        x *= a - i;
    }
    int y = 1;
    for(int i = 0; i < b; i++){
        y *= i + 1;
    }
    
    return x / y;
}

int ctoi(const char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default : return -1;
  }
}

int main(){
    char n[101];
    int k;
    scanf("%s", n);
    scanf("%d", &k);
    int keta;
    while(n[keta] != 0){
        keta++;
    }
    int ans = 0;
    // printf("%d\n", keta);
    // printf("%d\n", ctoi(n[0]));
    if (k == 1){
        for(int i = k; i <= keta; i++){
            if(i == keta){
                ans += ctoi(n[0]);
            }else if (k < i < keta) {
                ans += 9;
            }else if (i == k){
                ans += 9;
            }
        }
    }
    else if (k == 2){
        for(int i = k; i <= keta; i++){
            if(i == keta){
                ans += (ctoi(n[0]) - 1) * comb(i-1, k-1) * 9;
                ans += comb(i-2, k-1) * 9;
                ans += ctoi(n[1]);
            }else if (k < i < keta) {
                ans += comb(i-1, k-1) * 9 * 9;
            }else if (i == k){
                ans += 9 * 9;
            }
        }
    }
    else{
        for(int i = k; i <= keta; i++){
            if(i == keta){
                ans += (ctoi(n[0]) - 1) * comb(i-1, k-1) * 9 * 9;
                ans += comb(i-2, k-1) * 9 * 9;
                ans += ctoi(n[1] - 1) * comb(i-2, k-2) * 9;
                ans += comb(i-3, k-2) * 9;
                ans += ctoi(n[2]);
            }else if (k < i < keta) {
                ans += comb(i-1, k-1) * 9 * 9 * 9;
            }else if (i == k){
                ans += 9 * 9 * 9;
            }
        }
    }
    

    printf("%d\n", ans);
}