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.math.BigInteger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.*;
import java.util.Map.Entry;

public class Main{
	static int mod = (int) 1e9 + 7;

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		String s = scn.next();
		int d = scn.nextInt();
		int a[] = new int[s.length()];
		for (int i = 0; i < s.length(); i++) {
			a[i] = s.charAt(i) - '0';
		}
		long dp[][][] = new long[a.length][d][2];
		for (long v[][] : dp) {
			for (long c[] : v) {
				Arrays.fill(c, -1);
			}
		}
		long ans = (solve(0, 0, 1, a, dp, d) - 1+mod)%mod;
		System.out.println(ans);

	}

	private static long solve(int pos, int rem, int flag, int[] a, long[][][] dp, int d) {
		if (pos == a.length) {
			return rem == 0 ? 1 : 0;
		}
		if (dp[pos][rem][flag] != -1) {
			return dp[pos][rem][flag];
		}
		long res = 0;
		int limit = (flag == 1) ? a[pos] : 9;
		for (int i = 0; i <= limit; i++) {
			if (i == limit && flag==1) {
				res = (res + solve(pos + 1, (rem + i) % d, 1, a, dp, d)) % mod;
			} else {
				res = (res + solve(pos + 1, (rem + i) % d, 0, a, dp, d)) % mod;
			}

		}
		dp[pos][rem][flag] = res%mod;
		return res;
	}

}