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 MAXN 10010
#define MAXD 111
#define M 1000000007
#define int long long
using namespace std;
inline int mod(int a,int m){
	int r = a%m;
	if(r<0)r+=m;
	return r;
}
int dp[MAXN][MAXD];

int dp2[MAXN][MAXD][2];

int tamK=0;
string K;
int D;

/*
Dp(i,d,f) = dp(i+1,(d+number)%m, true) se K[i]=number 
Ou dp(i+1, (d+number)%m, false) se K[i] > number
*/

int solve2(int digitos,int num){
	//cout<<digitos<<" "<<num<<endl;
	if(dp[digitos][num]!=-1)return dp[digitos][num];
	if(digitos==tamK){
		return 0;
	}
	int res=0;
	if(mod(num,D)==0)res=1;
	for(int i=0;i<10;i++){
		if(digitos==1 && i==0)continue;
		res=mod(res+solve2(digitos+1,mod(num+i,D)),M);
	}
	return dp[digitos][num] = res;
}

int solve(int digitos,int num,int flag){
	
	if(dp2[digitos][num][flag]!=-1)return dp2[digitos][num][flag];
	
//	cout<<digitos<<" "<<num<<" "<<flag<<endl;
	
	if(digitos==tamK){
		if(flag==1 && mod(num,D)==0){
		//	cout<<"flag "<<digitos<<" "<<num<<" "<<endl;
			return 1;
		}
		return 0;
	}
	int res=0;

	for(int i=0;i<10;i++){
		if(flag==1){
			res=mod(res+solve(digitos+1,mod(num+i,D),1),M);
			continue;
		}
		
			
		if(K[digitos]-'0'>i){
		//	cout<<"loop "<<K[digitos]-'0'<<" "<<i<<endl;
			res=mod(res+solve(digitos+1,mod(num+i,D),1),M);
		}else if(digitos==tamK-1 && K[digitos]-'0'==i)res=mod(res+solve(digitos+1,mod(num+i,D),1),M);
		else if(K[digitos]-'0'==i)res=mod(res+solve(digitos+1,mod(num+i,D),0),M);
	}
	return dp2[digitos][num][flag] = res;
}

int32_t main(){
	memset(dp,-1,sizeof(dp));
	
	memset(dp2,-1,sizeof(dp2));
	cin>>K;
	cin>>D;
	tamK=K.size();
	int res =-1;
	//tirar o caso 000
	for(int i=0;i<10;i++){
		res=mod(res+solve2(1,mod(i,D)),M);
	}
//	cout<<res<<endl;
		
	for(int i=1;i<10;i++){
		if(i<K[0]-'0'){
		
		res=mod(res+solve(1,mod(i,D),1),M);
//		cout<<i<<" "<<res<<endl;
		}else if(i==K[0]-'0'){
			res=mod(res+solve(1,mod(i,D),0),M);
//			cout<<i<<" "<<res<<" "<<mod(i,D)<<endl;
		}
	//	cout<<i<<" "<<res<<endl;
	}
	
	cout<<res<<endl;
}