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 Rust (1.15.1) (judge verdict: Accepted)

use std::error::Error;
use std::io;

pub fn main() {
    if let Err(_e) = run() {
        panic!("failed.");
    }
}

fn run() -> Result<(), Box<Error>> {
    let mut input = String::new();
    io::stdin().read_line(&mut input);
    let k = input.trim().to_owned();
    input.clear();
    io::stdin().read_line(&mut input);
    let d = input.trim().parse::<usize>().unwrap();
    input.clear();

    let ks: Vec<char> = k.chars().collect();
    // 0 is not max, 1 is max.
    let mut dp = vec![vec![vec![-1; ks.len()]; d as usize]; 2];
    let mut result = solve(&mut dp, 0, 0, true, d, &ks);
    // delete all 0
    result = result + 1_000_000_007;
    result -= 1;
    result %= 1_000_000_007;
    println!("{}", result);
    Ok(())
}

fn solve(
    dp: &mut Vec<Vec<Vec<i64>>>,
    index: usize,
    remain: usize,
    is_max: bool,
    d: usize,
    k: &[char],
) -> i64 {
    if index == k.len() {
        if remain == 0 {
            return 1;
        } else {
            return 0;
        }
    }
    let first_index = if is_max { 1 } else { 0 };
    if dp[first_index][remain][index] != -1 {
        return dp[first_index][remain][index];
    }

    if is_max {
        let mut result = 0;
        let number = k[index].to_digit(10).unwrap() as usize;
        for i in 0..number {
            let new_remain = (remain + i) % d;
            result += solve(dp, index + 1, new_remain, false, d, k);
            result %= 1_000_000_007;
        }
        let new_remain = (remain + number) % d;
        result += solve(dp, index + 1, new_remain, true, d, k);
        result %= 1_000_000_007;
        dp[1][remain][index] = result;
        return result;
    }

    let mut result = 0;
    for i in 0..10 {
        let new_remain = (remain + i) % d;
        result += solve(dp, index + 1, new_remain, false, d, k);
        result %= 1_000_000_007;
    }
    dp[0][remain][index] = result;
    result
}