import strutils, sequtils, algorithm, strscans, future {.warning[SmallLshouldNotBeUsed]: off.} const MOD = 1000000007 proc minus1(a: var string): string = for i in countDown(a.len-1, 0): if a[i] != '0': a[i] = ((a[i].int - '0'.int) - 1 + '0'.int).char break else: a[i] = '9' while a.len > 1 and a[0] == '0': a = a[1..a.len-1] result = a proc solve(a: string, dp: var seq[seq[seq[seq[seq[int]]]]], pp: int): int = dp[0][0][0][0][0] = 1 for i in 0.. (a[i].int - '0'.int): break var p = if n < (a[i].int - '0'.int): 1 else: j q = if n == 3: 1 else: k dp[i+1][p][q][(n + l*10) mod 3][(n + m*10) mod pp] = (dp[i+1][p][q][(n + l*10) mod 3][(n + m*10) mod pp] + dp[i][j][k][l][m]) mod MOD for j in 0..1: for k in 0..1: for l in 0..2: for m in 0..pp-1: if (k == 1 or l == 0) and m != 0: result = (result + dp[a.len][j][k][l][m]) mod MOD when isMainModule: var input = stdin.readLine.split (a, b, p) = (input[0].minus1, input[1], input[2].parseint) dpA = newSeqWith(a.len+1, newSeqWith(2, newSeqWith(2, newSeqWith(3, newSeq[int](p))))) dpB = newSeqWith(b.len+1, newSeqWith(2, newSeqWith(2, newSeqWith(3, newSeq[int](p))))) var ansA = solve(a, dpA, p) ansB = solve(b, dpB, p) echo((ansB - ansA + MOD) mod MOD)