Problem:
Score : 100 points

Problem Statement

Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7:

The sum of the digits in base ten is a multiple of D.

Constraints

All values in input are integers.

1 \leq K < 10^{10000}

1 \leq D \leq 100

Input

Input is given from Standard Input in the following format:

K
D

Output

Print the number of integers satisfying the condition, modulo 10^9 + 7.

Sample Input 1

30
4

Sample Output 1

6

Those six integers are: 4, 8, 13, 17, 22 and 26.

Sample Input 2

1000000009
1

Sample Output 2

2

Be sure to print the number modulo 10^9 + 7.

Sample Input 3

98765432109876543210
58

Sample Output 3

635270834

Solution in Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

class Integer
  def digit_sum_multiple_count(div, mod = 10 ** 9 + 7)
    s = self.to_s
    l = s.size

    dpA = Array.new(l + 1) { Array.new(div, 0) }
    dpB = Array.new(l + 1) { Array.new(div, 0) }
    dpA[0][0] = 1

    l.times do |i|
      n = s[i].to_i

      0.upto(div - 1) do |j|
        if dpA[i][j] > 0
          dpA[i + 1][(j + n) % div] = dpA[i][j]

          if i > 0
            0.upto(n - 1) do |k|
              dpB[i + 1][(j + k) % div] += 1
            end
          end
        end

        if dpB[i][j] > 0
          0.upto(9) do |k|
            dpB[i + 1][(j + k) % div] += dpB[i][j]
            dpB[i + 1][(j + k) % div] %= mod
          end
        end
      end

      1.upto(9) do |k|
        next if i == 0 && k >= n
        dpB[i + 1][k % div] += 1
      end
    end

    (dpA[l][0] + dpB[l][0]) % mod
  end
end

K = gets.to_i
D = gets.to_i

puts K.digit_sum_multiple_count(D)