#include #include #include #include #include #include #define _fetch(_1, _2, _3, _4, name, ...) name #define rep2(i, n) rep3(i, 0, n) #define rep3(i, a, b) rep4(i, a, b, 1) #define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c)) #define rep(...) _fetch(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__) using namespace std; using i64 = long long; using u32 = unsigned; using u64 = unsigned long long; using f80 = long double; char S[10010]; u64 dp[2][20000]; struct fast_div { // < 2^31 fast_div(u32 n) : m(n) { s = (n == 1) ? 0 : 63 - __builtin_clz(n - 1); x = ((u64(1) << s) + n - 1) / n; } friend u32 operator / (u32 n, fast_div d) { return (u64(n) * d.x) >> d.s; } friend u32 operator % (u32 n, fast_div d) { return n - n / d * d.m; } u32 x, s, m; }; void solve() { const u32 MOD = 1e9 + 7; int M; while (~scanf("%s %d", S, &M)) { auto *curr = dp[0], *next = dp[1]; auto D = fast_div(M); int len = strlen(S); u64 ans = 0; fill(curr, curr + M, 0); rep(i, len) { copy(curr, curr + M, next); int d = S[i] - '0'; if (d > 0) next[d % D] += 1; else ans += 1; rep(j, M) next[(j * 10 + d) % D] += curr[j]; swap(curr, next); if (i % 8 == 0) rep(j, M) curr[j] %= MOD; } ans = (ans + curr[0]) % MOD; printf("%llu\n", ans); } } int main() { clock_t beg = clock(); solve(); clock_t end = clock(); fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC); return 0; }