-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathS-Digit_Sum.cpp
58 lines (46 loc) · 1.15 KB
/
S-Digit_Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e9 + 7;
const int N = (int)1e4;
// Concept used: Digit DP
int dp[N][2][100];
int f(int ind, int is_tight, int sum_of_dig, string &k, int &d)
{
sum_of_dig %= d;
if(ind >= k.size())
return ((sum_of_dig % d == 0) ? 1 : 0);
if(dp[ind][is_tight][sum_of_dig] != -1)
return dp[ind][is_tight][sum_of_dig];
int ans = 0;
int curr_dig = k[ind] - '0';
if(is_tight)
{
for(int i = 0; i <= curr_dig; i++)
{
if(i == curr_dig)
ans += f(ind + 1, 1, i + sum_of_dig, k, d);
else
ans += f(ind + 1, 0, i + sum_of_dig, k, d);
ans %= M;
}
}
else
{
for(int i = 0; i <= 9; i++)
{
ans += f(ind + 1, 0, i + sum_of_dig, k, d);
ans %= M;
}
}
return dp[ind][is_tight][sum_of_dig] = ans % M;
}
int32_t main()
{
string k;
int d;
cin >> k >> d;
memset(dp, -1, sizeof(dp));
cout << (f(0, 1, 0, k, d) - 1 + M) % M;
// subtracting 1 because '0' would have been counted as an answer too
}