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: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define int long long

int n;
string num;
int k;
int res = 0;
int C[123][4];
int dp[123][4];

int dfs(int pos, int left) {
    if (left == 0) return 1;
    if (pos > n) return 0;
    int ss = 0;
    for (int i = 0; i < num[pos - 1] - '0'; i++) {
        if (i == 0) ss += dp[n - pos][left];
        else ss += dp[n - pos][left - 1];
    }
    if (num[pos - 1] > '0') return ss + dfs(pos + 1, left - 1);
    else return ss + dfs(pos + 1, left);
}

signed main() {
    C[0][0] = 1;
    for (int i = 1; i <= 122; i++) {
        C[i][0] = 1;
        C[i][1] = i;
    }
    for (int i = 2; i <= 122; i++) {
        for (int j = 2; j <= min(3ll, i); j++) {
            C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
        }
    }
    ios::sync_with_stdio(false);
    cin >> num >> k;
    n = num.length();
    for (int i = 1; i < n; i++) {
        if (i < k) continue;
        res += 9 * C[i - 1][k - 1];
    }
    for (int i = 0; i <= 122; i++) {
        dp[i][0] = 1;
    }
    for (int i = 1; i <= 122; i++) {
        dp[i][1] = 9ll * C[i][1];
        if (i >= 2) dp[i][2] = 9ll * 9ll * C[i][2];
        if (i >= 3) dp[i][3] = 9ll * 9ll * 9ll * C[i][3];
    }
    cout << dfs(1, k) << endl;
    return 0;
}