// yukicoder: 315 世界のなんとか3.5 // 2019.8.30 bal4u #include #include typedef long long ll; #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } int ins(char *s) { // 文字列の入力 スペース以下の文字で入力終了 char *p = s; int c; do c = gc(), *s++ = c & 0xf; while (c > ' '); *--s = 0; return s - p; } #define MOD 1000000007 char AA[200005], *A; int WA; char B[200005]; int WB; int dp[200005][2][3][2]; // 3である桁, mod 3, 元の数 int dq[10][2][3][805][2]; // 3である桁, mod 3, mod 800, 元の数 int C8[4] = {0,1,2,4}; int C80[5] = {0,1,10,20,40}; int C800[6] = {0,1,10,100,200,400}; inline static void chmod(int *a, int b) { *a = ((ll)(*a) + b) % MOD; } int calc(int w, char *s, int *C, int L, int P) { int i, j, i3, m3, m8, k; int ww; memset(dp, 0, sizeof(dp)); dp[0][0][0][1] = 1; ww = w-L; if (ww < 0) ww = 0, L = w; for (i = 0; i < ww; i++) for (i3 = 0; i3 < 2; i3++) for (m3 = 0; m3 < 3; m3++) for (k = 0; k < 2; k++) if (dp[i][i3][m3][k]) { if (k) { for (j = s[i]-1; j >= 0; j--) chmod(&dp[i+1][i3|(j==3)][(m3+j)%3][0], dp[i][i3][m3][k]); chmod(&dp[i+1][i3|(s[i]==3)][(m3+s[i])%3][k], dp[i][i3][m3][k]); } else { for (j = 9; j >= 0; j--) chmod(&dp[i+1][i3|(j==3)][(m3+j)%3][k], dp[i][i3][m3][k]); } } memset(dq, 0, sizeof(dq)); for (i3 = 0; i3 < 2; i3++) for (m3 = 0; m3 < 3; m3++) for (k = 0; k < 2; k++) dq[0][i3][m3][0][k] = dp[ww][i3][m3][k]; for (i = 0; i < L; i++) for (i3 = 0; i3 < 2; i3++) for (m3 = 0; m3 < 3; m3++) for (m8 = 0; m8 < P; m8++) for (k = 0; k < 2; k++) if (dq[i][i3][m3][m8][k]) { if (k) { for (j = s[i+ww]-1; j >= 0; j--) { chmod(&dq[i+1][i3|(j==3)][(m3+j)%3][(m8+C[L-i]*j)%P][0], dq[i][i3][m3][m8][k]); } chmod(&dq[i+1][i3|(s[i+ww]==3)][(m3+s[i+ww])%3][(m8+C[L-i]*s[i+ww])%P][k], dq[i][i3][m3][m8][k]); } else { for (j = 9; j >= 0; j--) { chmod(&dq[i+1][i3|(j==3)][(m3+j)%3][(m8+C[L-i]*j)%P][k], dq[i][i3][m3][m8][k]); } } } k = 0; for (m8 = 1; m8 < P; m8++) chmod(&k, dq[L][0][0][m8][0] + dq[L][0][0][m8][1]); for (m3 = 0; m3 < 3; m3++) for (m8 = 1; m8 < P; m8++) chmod(&k, dq[L][1][m3][m8][0] + dq[L][1][m3][m8][1]); return k; } int main() { int i, P, ans; int L, *C; WA = ins(A = AA), WB = ins(B), P = in(); if (P == 8) C = C8, L = 3; else if (P == 80) C = C80, L = 4; else if (P == 800) C = C800, L = 5; if (WA == 1 && *A == 1) ans = calc(WB, B, C, L, P); else { for (i = WA-1; i >= 0; i--) { if (A[i] > 0) { A[i]--; break; } A[i] = 9; } if (A[0] == 0) WA--, A++; ans = calc(WB, B, C, L, P) - calc(WA, A, C, L, P); } if (ans < 0) ans += MOD; printf("%d\n", ans); return 0; }