import std.algorithm, std.conv, std.range, std.stdio, std.string; const p = 10 ^^ 9 + 7; void main() { auto rd1 = readln.split; auto rd2 = rd1[0..2].map!(s => s.map!(c => (c - '0').to!int).array), a = rd2[0], b = rd2[1]; auto p = rd1[2].to!int; auto sp = p.predSwitch(8, 0, 80, 1, 800, 2); auto r = calc8!true(b, sp); auto s = calc8!false(a, sp); r.sub(s); writeln(r); } auto calc8(bool includeMax)(int[] ai, int sp) { auto n = ai.length.to!int; auto dp1 = new int[][][][](2, 2, 3, 8); dp1[0][0][0][0] = 1; foreach (i; 0..n-sp) { auto dp2 = new int[][][][](2, 2, 3, 8); foreach (j; 0..2) foreach (k; 0..2) foreach (l; 0..3) foreach (m; 0..8) { int lim = j ? 9 : ai[i]; foreach (d; 0..lim+1) dp2[j || d < lim][k || d == 3][(l + d) % 3][(m * 10 + d) % 8].add(dp1[j][k][l][m]); } dp1 = dp2; } auto dp3 = new int[][][][][][](2, 2, 3, 8, 2, 2); foreach (j; 0..2) foreach (k; 0..2) foreach (l; 0..3) foreach (m; 0..8) dp3[j][k][l][m][0][0] = dp1[j][k][l][m]; foreach_reverse (i; 0..min(sp, ai.length)) { auto dp4 = new int[][][][][][](2, 2, 3, 8, 2, 2); foreach (j; 0..2) foreach (k; 0..2) foreach (l; 0..3) foreach (m; 0..8) { int lim = j ? 9 : ai[$-i-1]; foreach (d; 0..lim+1) foreach (p; 0..2) foreach (q; 0..2) dp4[j || d < lim][k || d == 3][(l + d) % 3][m][q][d == 0].add(dp3[j][k][l][m][p][q]); } dp3 = dp4; } auto jMin = includeMax ? 0 : 1, r = 0; foreach (j; jMin..2) foreach (k; 0..2) foreach (l; 0..3) foreach (m; 0..8) foreach (p; 0..2) foreach (q; 0..2) if ((k == 1 || l == 0) && (sp == 0 && m != 0 || sp == 1 && (m != 0 || q == 0) || sp == 2 && (m != 0 || p == 0 || q == 0))) r.add(dp3[j][k][l][m][p][q]); return r; } auto add(ref int a, int b) { a = (a + b) % p; } auto sub(ref int a, int b) { a = ((a - b) % p + p) % p; }