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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

class Main {
	static final int M = 1000000007;

	public static void main(String[] args) throws Exception {
		final Scanner sc = new Scanner(System.in);
		String k = sc.next();
		int d = sc.nextInt();

		int[][][] dp = new int[k.length() + 1][101][2];
		dp[0][0][0] = 1;

		for (int i = 0; i < k.length(); i++) {
			for (int j = 0; j < d; j++) {
				for (int l = 0; l < 2; l++) {
					int c = Character.digit(k.charAt(i), 10);
					for (int next = 0; next < 10; next++) {
						if (next < c) dp[i + 1][(j + next) % d][1] += dp[i][j][l];
						else if (next == c) dp[i + 1][(j + next) % d][l] += dp[i][j][l];
						else if (l == 1) dp[i + 1][(j + next) % d][l] += dp[i][j][l];
						dp[i + 1][(j + next) % d][0] %= M;
						dp[i + 1][(j + next) % d][1] %= M;
					}
				}
			}
		}

		System.out.println((((dp[k.length()][0][0] - 1 + M) % M) + dp[k.length()][0][1]) % M);
	}
}