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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// input scanner
var sc = bufio.NewScanner(os.Stdin)

const (
	initialBufSize = 10e4
	maxBufSize     = 10e6
)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextInt64() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

// util
func max(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n > ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func min(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n < ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func combi(n int, k int) int {
	a := 1
	b := 1
	for i := 0; i < k; i++ {
		a *= n - i
		b *= (k - i)
	}
	return a / b
}

func check(s string, k int) bool {
	l := len(s)
	var c int
	for i := 0; i < l; i++ {
		a, _ := strconv.Atoi(s[i : i+1])
		if a != 0 {
			c++
		}
		if c > k {
			return false
		}
	}
	if c == k {
		return true
	}
	return false
}

func run(s string, k int) int {
	l := len(s)
	dp := make([][]int, l)
	dpm := make([][]int, l)
	a := make([]int, l)
	for i := 0; i < l; i++ {
		a[i], _ = strconv.Atoi(s[i : i+1])
		dp[i] = make([]int, 3+1)
		dpm[i] = make([]int, 3+1)
	}
	// init
	for i := 1; i < l; i++ {
		dp[i][0] = 1
	}
	dp[0][1] = a[0]
	dp[0][2] = 0
	dp[0][3] = 0

	for i := 1; i < l; i++ {
		for j := 1; j <= 3; j++ {
			if dp[i-1][j-1] == 0 {
				dp[i][j] = 0
			} else if check(s[0:i], j-1) {
				dp[i][j] = dp[i-1][j] + dp[i-1][j-1]*9 - (9 - a[i])
			} else {
				dp[i][j] = dp[i-1][j] + dp[i-1][j-1]*9
			}
		}
	}
	return dp[l-1][k]
}

// main
func main() {
	buf := make([]byte, initialBufSize)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	s := next()
	k := nextInt()
	fmt.Println(run(s, k))
}