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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define PB push_back

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
typedef vector < ll > vl;

const int MAXD = 105;
const int MAXN = 10005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int LOG = 18;
const double EPS = 1e-9;
const double PI = 3.1415926535;

int dp[MAXN][MAXD];
// stanje:
// suma modulo D
// na kojoj smo znamenci
// jesan li dosad maksimalan broj ili ne

int add(const int& a, const int& b) {
    if(a + b >= MOD)
        return (a + b - MOD);
    return (a + b);
}

int subt(const int& a, const int& b) {
    if(a - b < 0)
        return (a - b + MOD);
    return (a - b);
}

int main() {
    int D, ans;
    string K;

    cin >> K >> D;
    int sz = K.size();

    if(K.size() == 1) {
        int cnt = 0;
        for(int i = 1; i <= K[0] - '0'; i++)
            if(i % D == 0)
                cnt ++;
        cout << cnt << endl;
        return 0;
    }

    for(int i = 0; i <= K[0] - '0' - 1; i++)
        dp[0][i] = 1;                         // inicijaliziras prvu znamenku svih koji su manji od 1. znam
    int sum_of_max = (K[0] - '0') % D;        // inicijaliziras ako je prva znamenka jednako velika ko broj

    for(int i = 0; i < sz - 1; i++) {
        int next_znam = K[i + 1] - '0';
        for(int j = 0; j < D; j++)
            for(int znam = 0; znam <= 9; znam++)
                dp[i + 1][(j + znam) % D] = add(dp[i + 1][(j + znam) % D], dp[i][j]);

        for(int znam = 0; znam <= next_znam - 1; znam++)
            dp[i + 1][(sum_of_max + znam) % D] = add(dp[i + 1][(sum_of_max + znam) % D], 1);
        sum_of_max = (sum_of_max + next_znam) % D;
    }

    if(sum_of_max == 0)                             // (+1) od K i (-1) od 0 se ubiju
        cout << dp[sz - 1][0] << endl;
    else                                            // inace makni 0
        cout << subt(dp[sz - 1][0], 1) << endl;

    return 0;
}