#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) { int s; for (int j=i+1;j> 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; }