#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) typedef long long ll; int const inf = 1<<29; int const MOD = 1e9+7; ll dp1[2][2][3][2]; // index, less, mod3, used3 ll dp2[2][2][3][2][800]; // index, less, mod3, used3, modP int P; string A, B; unordered_map simo = {{8, 3}, {80, 4}, {800, 5}}; ll f(string const& S, bool last) { int N = S.size(); zero(dp2); if(N-simo[P] > 0) { zero(dp1); dp1[0][0][0][0] = 1; rep(i, N-simo[P]) { rep(less, 2) rep(mod3, 3) rep(used3, 2) { int dmax = less ? 9 : S[i] - '0'; rep(d, dmax+1) { auto const& curr = dp1[i&1][less][mod3][used3]; auto const ni = (i+1)&1; auto const nless = less || (d < dmax); auto const nmod3 = (mod3 * 10 + d) % 3; auto const nused3 = used3 || (d == 3); auto& next = dp1[ni][nless][nmod3][nused3]; next += curr; next %= MOD; } } zero(dp1[i&1]); } rep(j, 2) rep(k, 3) rep(l, 2) { dp2[(N-simo[P])&1][j][k][l][0] = dp1[(N-simo[P])&1][j][k][l]; } } else { dp2[0][0][0][0][0] = 1; } REP(i, max(0, N-simo[P]), N) { rep(less, 2) rep(mod3, 3) rep(used3, 2) rep(modP, P) { int dmax = less ? 9 : S[i] - '0'; rep(d, dmax+1) { auto const& curr = dp2[i&1][less][mod3][used3][modP]; auto const ni = (i+1)&1; auto const nless = less || (d < dmax); auto const nmod3 = (mod3 * 10 + d) % 3; auto const nused3 = used3 || (d == 3); auto const nmodP = (modP * 10 + d) % P; auto& next = dp2[ni][nless][nmod3][nused3][nmodP]; next += curr; next %= MOD; } } zero(dp2[i&1]); } int ret = 0; rep(less, 2) rep(mod3, 3) rep(used3, 2) rep(modP, P) { if(!(mod3 == 0 || used3)) { continue; } if(modP == 0) { continue; } ret += dp2[N&1][less][mod3][used3][modP]; ret %= MOD; } if(last) { rep(mod3, 3) rep(used3, 2) rep(modP, P) { if(!(mod3 == 0 || used3)) { continue; } if(modP == 0) { continue; } ret -= dp2[N&1][0][mod3][used3][modP]; ret += MOD; ret %= MOD; } } return ret; } int main() { string A, B; cin >> A >> B >> P; cout << (f(B, false) - f(A, true) + MOD) % MOD << endl; return 0; }