#include #include using namespace std; const int md = 1e9 + 7; long long digitdp(vector& aa, bool eq, int p) { int k = to_string(p).length(); p = stoi(to_string(p)); long long res = 0; vector> dp(2, vector(24, 0)); int jf = 0, jm = 0; for (size_t i = 0; i < aa.size(); ++i) { vector> ndp(2, vector(24, 0)); // Transition from less than confirmed for (int f = 0; f < 2; ++f) { for (int m = 0; m < 24; ++m) { long long pre = dp[f][m] % md; if (pre == 0) continue; for (int d = 0; d < 10; ++d) { int nf = f | (d == 3); int nm = (m * 10 + d) % 24; ndp[nf][nm] += pre; } } } // Transition from exactly for (int d = (i == 0); d < aa[i]; ++d) { int nf = jf | (d == 3); int nm = (jm * 10 + d) % 24; ndp[nf][nm] += 1; } // Start from this digit if (i) { for (int d = 1; d < 10; ++d) { ndp[d == 3][d] += 1; } } // Update exactly jf |= (aa[i] == 3); jm = (jm * 10 + aa[i]) % 24; dp = ndp; if (i == aa.size() - k) { for (int f = 0; f < 2; ++f) { for (int m = 0; m < 24; ++m) { if ((f || m % 3 == 0) && m % 8 == 0) { res -= dp[f][m]; res %= md; } } } } } if (eq) { long long pos = 0; int l = max(0, (int)aa.size() - 5); for (size_t i = l; i < aa.size(); ++i) { pos = (pos * 10 + aa[i]) % p; } if ((jf || jm % 3 == 0) && pos) res += 1; } for (int f = 0; f < 2; ++f) { for (int m = 0; m < 24; ++m) { if (f || m % 3 == 0) { res += dp[f][m]; res %= md; } } } return res; } int main() { string a, b, p; cin >> a >> b >> p; vector aa(a.size()), bb(b.size()); for (size_t i = 0; i < a.size(); ++i) aa[i] = a[i] - '0'; for (size_t i = 0; i < b.size(); ++i) bb[i] = b[i] - '0'; int k = p.size(); long long ans = digitdp(bb, true, stoi(p)) - digitdp(aa, false, stoi(p)); cout << (ans % md + md) % md << endl; return 0; }