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: Accepted)

#pragma gcc optimize("O3")
#include <bits/stdc++.h>
using namespace std;
const int MOD=1000000007;
int d,a,b,i2,pos;
long long dp[102],res,tmp[102];
char k[10002];
int main()
{
    scanf("%s",&k);
    for(; k[pos]!=0; ++pos) a+=k[pos]-'0';
    scanf("%d",&d);
    dp[0]=1;
    a%=d;
    if(a==0) ++res;
    while(--pos>=0){
        if(k[pos]!='0'){
            b=k[pos]-'0';
            a-=b;
            while(a<0)
                a+=d;
            if(a)
                i2=d-a;
            else
                i2=0;
            for(int i=0; i<b; ++i){
                res+=dp[i2--];
                if(i2<0) i2+=d;
            }
            while(res>=MOD) res-=MOD;
        }
        tmp[0]=0;
        i2=d-1;
        for(int i=0; i<9; ++i){
            tmp[0]+=dp[i2--];
            if(i2<0) i2+=d;
            if(tmp[0]>=MOD) tmp[0]-=MOD;
        }
        for(int i=1; i<d; ++i) tmp[i]=tmp[i-1]+dp[i-1]-dp[++i2%d];
        for(int i=0; i<d; ++i){
            dp[i]=dp[i]+tmp[i];
            if(dp[i]>=MOD){
                dp[i]-=MOD;
                while(dp[i]>=MOD) dp[i]-=MOD;
            }
            else if(dp[i]<0){
                dp[i]+=MOD;
                while(dp[i]<0) dp[i]+=MOD;
            }
        }
    }
    if(--res<0) res+=MOD;
    printf("%d",res);
    return 0;
}