/* * No.260 世界のなんとか3 * * 解法) * ways[i+1][a_greater][b_greater][mod3][mod8][has_three] */ #include #include #include using namespace std; const int kMOD = 1000 * 1000 * 1000 + 7; const int kMAX_INDEX = 10010; string A, B; int ways[kMAX_INDEX][2][2][3][8][2]; void Solve() { vector int_A, int_B; // AとBを数列に変換する for (int i = 0; i < B.size(); i++) { if (i + A.size() < B.size()) int_A.push_back(0); else int_A.push_back(A[i - B.size() + A.size()] - '0'); int_B.push_back(B[i] - '0'); } // 桁dp ways[0][0][0][0][0][0] = 1; for (int i = 0; i < int_A.size(); i++) { for (int greater = 0; greater < 2; greater++) { for (int smaller = 0; smaller < 2; smaller++) { for (int mod3 = 0; mod3 < 3; mod3++) { for (int mod8 = 0; mod8 < 8; mod8++) { for (int has_three = 0; has_three < 2; has_three++) { if (ways[i][greater][smaller][mod3][mod8][has_three] == 0) continue; for (int digit = 0; digit < 10; digit++) { if (!greater && digit < int_A[i]) continue; if (!smaller && digit > int_B[i]) break; ways[i + 1][greater | digit > int_A[i]][smaller | digit < int_B[i]][(mod3 * 10 + digit) % 3][(mod8 * 10 + digit) % 8][has_three | digit == 3] += ways[i][greater][smaller][mod3][mod8][has_three]; ways[i + 1][greater | digit > int_A[i]][smaller | digit < int_B[i]][(mod3 * 10 + digit) % 3][(mod8 * 10 + digit) % 8][has_three | digit == 3] %= kMOD; } } } } } } } int answer = 0; for (int greater = 0; greater < 2; greater++) { for (int smaller = 0; smaller < 2; smaller++) { // 3で割りきれる for (int mod8 = 1; mod8 < 8; mod8++) { for (int has_three = 0; has_three < 2; has_three++) { answer = (answer + ways[int_A.size()][greater][smaller][0][mod8][has_three]) % kMOD; } } // 3で割り切れない for (int mod3 = 1; mod3 < 3; mod3++) { for (int mod8 = 1; mod8 < 8; mod8++) { answer = (answer + ways[int_A.size()][greater][smaller][mod3][mod8][1]) % kMOD; } } } } cout << answer << endl; } int main() { cin >> A >> B; Solve(); return 0; }