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 LuaJIT (2.0.4) (judge verdict: Accepted)

local mod = 1000000007
local function badd(x, y) return (x + y) % mod end

local s = io.read()
local d = io.read("*n")
local dpmax, dpnormal = {}, {}
for i = 1, d do
  dpmax[i] = 0
  dpnormal[i] = 0
end
dpmax[d] = 1
local nxt_max, nxt_normal = {}, {}
for i = 1, #s do
  for j = 1, d do
    nxt_max[j] = 0
    nxt_normal[j] = 0
  end
  local v = s:sub(i, i):byte() - 48
  for src = 1, d do
    for add = 0, v do
      local dst = (add + src) % d
      if dst == 0 then dst = d end
      if add == v then
        nxt_max[dst] = badd(nxt_max[dst], dpmax[src])
      else
        nxt_normal[dst] = badd(nxt_normal[dst], dpmax[src])
      end
    end
    for add = 0, 9 do
      local dst = (add + src) % d
      if dst == 0 then dst = d end
      nxt_normal[dst] = badd(nxt_normal[dst], dpnormal[src])
    end
  end
  for j = 1, d do
    dpmax[j] = nxt_max[j]
    dpnormal[j] = nxt_normal[j]
  end
end
local ret = badd(dpmax[d], dpnormal[d])
ret = badd(ret, mod - 1)
print(ret)