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++ (GCC 9.2.1) (judge verdict: Memory Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
string K;
long long P = 1000000000LL+7LL;
long long D;

long long dp[1000010][110][2];

long long f(int pos, long long sum , bool sw){
    if(pos == K.size()){
        if(sum%D == 0) return 1;
        else return 0;
    }
    if(dp[pos][sum][sw]!=-1) return dp[pos][sum][sw];
    long long re = 0;
    long long i = 0;
    if(sw == 0){
        for(i ; i < 10 ; i++){
            re = (re%P  + f(pos+1,(sum%D+i%D)%D,0)%P)%P;
        }
    }else{
        for( i; i <= int(K[pos]-'0') ; i++){
            if(i == int(K[pos]-'0')){
                re=(re%P +f(pos+1,(sum%D+i)%D,1)%P)%P;
            } 
            else re = (re%P +f(pos+1,(sum%D+i)%D,0)%P)%P;
        }
    }
    dp[pos][sum][sw] = re;
    return re;
}
int main(){
    while(cin>>K>>D){
        memset(dp,-1,sizeof dp);
        cout<<f(0,0,1)-1<<"\n";
    }
    return 0;
}