import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } enum MO = 10L^^9 + 7; int N; string A, B; int P; long[][][][] small; long[][][] dp; long calc(int i, int x, int y, int z, bool diffA, bool diffB) { long ret; if (i == N || diffA && diffB) { foreach (x0; 0 .. 3) foreach (y0; 0 .. 2) { foreach (x1; 0 .. 3) foreach (y1; 0 .. 2) { if ((x0 + x1 + x) % 3 == 0 || (y0 || y1 || y)) { ret += small[min(N - i, 5)][x0][y0][z] * dp[max(N - 5 - i, 0)][x1][y1]; } } } } else { foreach (d; (diffA ? 0 : (A[i] - '0')) .. (diffB ? 9 : (B[i] - '0')) + 1) { int zz = z; if (N - 1 - i < 5) { zz += 10^^(N - 1 - i) * d; } ret += calc(i + 1, (x + d) % 3, (y || d == 3) ? 1 : 0, zz, (diffA || (d != A[i] - '0')), (diffB || (d != B[i] - '0'))); } } ret %= MO; debug { writefln("calc %s %s %s %s %s %s: %s", i, x, y, z, diffA, diffB, ret); } return ret; } void main() { try { for (; ; ) { A = readToken(); B = readToken(); P = readInt(); A = iota(B.length - A.length).map!(_ => '0').array.to!string ~ A; N = cast(int)(B.length); small = new long[][][][](6, 3, 2, 10^^5); foreach (i; 0 .. 5 + 1) { foreach (z; 0 .. 10^^5) { if (z % 10^^i == 0) { foreach (w; 0 .. 10^^i) { if ((z + w) % P != 0) { const x = w % 3; const y = iota(i).map!(j => (w / 10^^j % 10 == 3)).any; ++small[i][x][y][z]; } } } } } dp = new long[][][](N + 1, 3, 2); dp[0][0][0] = 1; foreach (i; 0 .. N) { foreach (x; 0 .. 3) foreach (y; 0 .. 2) { foreach (d; 0 .. 10) { const xx = (x + d) % 3; const yy = (y || d == 3) ? 1 : 0; if ((dp[i + 1][xx][yy] += dp[i][x][y]) >= MO) { dp[i + 1][xx][yy] -= MO; } } } } debug { writeln("dp = ", dp[0 .. min(10, $)]); } const ans = calc(0, 0, 0, 0, false, false); writeln(ans); } } catch (EOFException e) { } }