#include <bits/stdc++.h> using ll = long long; using namespace std; const int M = 9; ll l, r, dp[20][1 << M][5][7][8][9][2]; int nmask, nr5, nr7, nr8, nr9; int div(int n, vector<int> &x) { if (n == 1) return 1; if (n == 2) return x[2] % 2 == 0; if (n == 3) return x[3] % 3 == 0; if (n == 4) return x[2] % 4 == 0; if (n == 5) return !x[0]; if (n == 6) return x[2] % 2 == 0 && x[3] % 3 == 0; if (n == 7) return !x[1]; if (n == 8) return !x[2]; if (n == 9) return !x[3]; assert(0); } ll f(ll n) { if (n == 0) return 1; memset(dp, 0, sizeof dp); vector<int> dig; while (n != 0) { dig.push_back(n % 10); n /= 10; } reverse(dig.begin(), dig.end()); dp[0][0][0][0][0][0][1] = 1; for (int i = 0; i < dig.size(); ++i) for (int mask = 0; mask < 1 << M; ++mask) for (int r5 = 0; r5 < 5; ++r5) for (int r7 = 0; r7 < 7; ++r7) for (int r8 = 0; r8 < 8; ++r8) for (int r9 = 0; r9 < 9; ++r9) { if (!dp[i][mask][r5][r7][r8][r9][0] && !dp[i][mask][r5][r7][r8][r9][1]) continue; for (int c = (mask != 0); c <= 9; ++c) { nmask = mask | (c > 0 ? (1 << (c - 1)) : 0); nr5 = (10 * r5 + c) % 5; nr7 = (10 * r7 + c) % 7; nr8 = (10 * r8 + c) % 8; nr9 = (10 * r9 + c) % 9; dp[i + 1][nmask][nr5][nr7][nr8][nr9][0] += dp[i][mask][r5][r7][r8][r9][0]; if (c <= dig[i]) dp[i + 1][nmask][nr5][nr7][nr8][nr9][c == dig[i]] += dp[i][mask][r5][r7][r8][r9][1]; } } ll res = 0; for (int mask = 0; mask < 1 << M; ++mask) for (int r5 = 0; r5 < 5; ++r5) for (int r7 = 0; r7 < 7; ++r7) for (int r8 = 0; r8 < 8; ++r8) for (int r9 = 0; r9 < 9; ++r9) { vector<int> v = {r5, r7, r8, r9}; bool ok = 1; for (int b = 0; b < M; ++b) if (mask >> b & 1) ok &= div(b + 1, v); if (ok) for (int t = 0; t < 2; ++t) res += dp[dig.size()][mask][r5][r7][r8][r9][t]; } return res; } int main() { cin >> l >> r; cout << f(r) - f(l - 1) << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <bits/stdc++.h> using ll = long long; using namespace std; const int M = 9; ll l, r, dp[20][1 << M][5][7][8][9][2]; int nmask, nr5, nr7, nr8, nr9; int div(int n, vector<int> &x) { if (n == 1) return 1; if (n == 2) return x[2] % 2 == 0; if (n == 3) return x[3] % 3 == 0; if (n == 4) return x[2] % 4 == 0; if (n == 5) return !x[0]; if (n == 6) return x[2] % 2 == 0 && x[3] % 3 == 0; if (n == 7) return !x[1]; if (n == 8) return !x[2]; if (n == 9) return !x[3]; assert(0); } ll f(ll n) { if (n == 0) return 1; memset(dp, 0, sizeof dp); vector<int> dig; while (n != 0) { dig.push_back(n % 10); n /= 10; } reverse(dig.begin(), dig.end()); dp[0][0][0][0][0][0][1] = 1; for (int i = 0; i < dig.size(); ++i) for (int mask = 0; mask < 1 << M; ++mask) for (int r5 = 0; r5 < 5; ++r5) for (int r7 = 0; r7 < 7; ++r7) for (int r8 = 0; r8 < 8; ++r8) for (int r9 = 0; r9 < 9; ++r9) { if (!dp[i][mask][r5][r7][r8][r9][0] && !dp[i][mask][r5][r7][r8][r9][1]) continue; for (int c = (mask != 0); c <= 9; ++c) { nmask = mask | (c > 0 ? (1 << (c - 1)) : 0); nr5 = (10 * r5 + c) % 5; nr7 = (10 * r7 + c) % 7; nr8 = (10 * r8 + c) % 8; nr9 = (10 * r9 + c) % 9; dp[i + 1][nmask][nr5][nr7][nr8][nr9][0] += dp[i][mask][r5][r7][r8][r9][0]; if (c <= dig[i]) dp[i + 1][nmask][nr5][nr7][nr8][nr9][c == dig[i]] += dp[i][mask][r5][r7][r8][r9][1]; } } ll res = 0; for (int mask = 0; mask < 1 << M; ++mask) for (int r5 = 0; r5 < 5; ++r5) for (int r7 = 0; r7 < 7; ++r7) for (int r8 = 0; r8 < 8; ++r8) for (int r9 = 0; r9 < 9; ++r9) { vector<int> v = {r5, r7, r8, r9}; bool ok = 1; for (int b = 0; b < M; ++b) if (mask >> b & 1) ok &= div(b + 1, v); if (ok) for (int t = 0; t < 2; ++t) res += dp[dig.size()][mask][r5][r7][r8][r9][t]; } return res; } int main() { cin >> l >> r; cout << f(r) - f(l - 1) << endl; return 0; } |