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.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String number = scanner.next();
        int modulo = scanner.nextInt();
        long lots = 1000000007;

        long[][] exact = new long[number.length() + 1][modulo];
        long[][] less = new long[number.length() + 1][modulo];
        exact[0][0] = 1;

        for (int i = 1; i <= number.length(); ++i) {
            for (int m = 0; m < modulo; ++m) {
                for (int d = 0; d <= 9; ++d) {
                    less[i][m] += less[i - 1][mod(m - d, modulo)];
                    less[i][m] %= lots;
                }

                int digit = number.charAt(i - 1) - '0';
                for (int d = 0; d < digit; ++d) {
                    less[i][m] += exact[i - 1][mod(m - d, modulo)];
                    less[i][m] %= lots;
                }

                exact[i][m] += exact[i - 1][mod(m - digit, modulo)];
                exact[i][m] %= lots;
            }
        }

        long res = exact[number.length()][0] + less[number.length()][0];
        res = (res + lots - 1) % lots;

        System.out.println(res);
    }

    public static int mod(int val, int modulo) {
        int res = val % modulo;
        return res < 0 ? res + modulo : res;
    }

}