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 Python (3.4.3) (judge verdict: Time Limit Exceeded)

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np
from numpy.fft import rfft, irfft
MOD = 10 ** 9 + 7
K = [int(x) for x in input().rstrip()]
L = len(K)
D = int(input())

fft_len = 1
while fft_len < 2*D:
    fft_len *= 2

def fft_convolve(A,B):
    return np.round(irfft(rfft(A,fft_len) * rfft(B,fft_len))).astype(np.int64) % MOD

def convolve(A,B):
    M = 1 << 16
    mask = M - 1
    A1,A2 = A>>16, A&mask
    B1,B2 = B>>16, B&mask
    X,Y,Z = [fft_convolve(P,Q) for P,Q in [[A1,B1],[A1-A2,B1-B2],[A2,B2]]]
    X = (X << 32) + ((X-Y+Z) << 16) + Z
    X[:D] += X[D:D+D]
    return X[:D] % MOD

# d桁以下、自由に使った場合の余りごとの個数。0を含めて数えておく
ones = [np.zeros(D,dtype=np.int64)]
for x in range(10):
    arr = ones[-1].copy()
    arr[x%D] += 1
    ones.append(arr)
full = []
dp = np.zeros(D, dtype=np.int64)
dp[0] = 1
full.append(dp)
for d in range(1,L):
    dp = convolve(dp,ones[10])
    full.append(dp)

dist = np.zeros(D+D, dtype=np.int64)

cumsum = 0
for i,x in enumerate(K):
    arr = convolve(ones[x], full[L-i-1])
    dist[cumsum:cumsum+D] += arr
    cumsum += x
    cumsum %= D
dist[cumsum] += 1 # 全部そのまま使う

answer = dist[::D].sum() - 1 # 0除外
answer %= MOD
print(answer)