#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) #define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) using ll = long long; using namespace std; template auto vectors(X x, T a) { return vector(x, a); } template auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector(x, cont); } vector > strmodin(string const & s, int a, vector const & b, ll m) { int pow_b = 1 << b.size(); array table = {}; repeat (d,10) { auto it = whole(find, b, d); if (it != b.end()) table[d] |= 1 << (it - b.begin()); } auto cur = vectors(a, pow_b, ll()); auto prv = vectors(a, pow_b, ll()); int bound_i = 0, bound_j = 0; for (char c : s) { c -= '0'; cur.swap(prv); repeat (d, c) cur[(bound_i * 10 + d) % a][bound_j | table[d]] += 1; bound_i = (bound_i * 10 + c) % a; bound_j |= table[c]; repeat (i,a) repeat (j,pow_b) { repeat (d,10) { ll & it = cur[(i * 10 + d) % a][j | table[d]]; it = (it + prv[i][j]) % m; } prv[i][j] = 0; } } cur[bound_i][bound_j] += 1; cur[bound_i][bound_j] %= m; return cur; } const int mod = 1e9+7; ll f(string const & s, int p) { auto dp = strmodin(s, 3 * p, { 3 }, mod); ll cnt = 0; repeat (i,3*p) repeat (j,2) { if ((i % 3 == 0 or j) and i % p != 0) { cnt += dp[i][j]; cnt %= mod; } } return cnt; } bool g(string const & s, int p) { auto modstr = [&](ll m) { ll q = 0; for (char c : s) q = (q * 10 + (c - '0')) % m; return q; }; return (modstr(3) == 0 or whole(count, s, '3')) and modstr(p) != 0; } int main() { string a, b; int p; cin >> a >> b >> p; cout << ((f(b, p) - f(a, p) + g(a, p)) % mod + mod) % mod << endl; return 0; }