#include <cstdio> #include <cstring> using i64 = long long; i64 dp[2][2][2][1 << 9][2520], pw[19]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } i64 solve(i64 n) { if (n <= 0) return 0; int l = 1; while (n / pw[l]) ++l; memset(dp, 0, sizeof(dp)); dp[l & 1][1][1][0][0] = 1; for (int i = l; i >= 1; --i) { int u = i & 1, v = u ^ 1; memset(dp[v], 0, sizeof(dp[v])); int o = n / pw[i - 1] % 10; for (int e = 0; e < 4; ++e) { int vl = 1, vr = (e % 2) ? o : 9; if (e / 2) vl = 0; for (int d = vl; d <= vr; ++d) { int ne = (e % 2) && d == o, nz = (e / 2) && d == 0; for (int mask = 0; mask < (1 << 9); ++mask) { int n_mask = mask | (d ? (1 << (d - 1)) : 0); for (int m = 0; m < 2520; ++m) { if (!dp[u][e % 2][e / 2][mask][m]) continue; dp[v][ne][nz][n_mask][(m * 10 + d) % 2520] += dp[u][e % 2][e / 2][mask][m]; } } } } } i64 ret = 0; for (int e = 0; e < 2; ++e) { for (int mask = 1; mask < (1 << 9); ++mask) { int lcm = 1; for (int i = 0; i < 9; ++i) { if (mask >> i & 1) lcm = lcm * (i + 1) / gcd(lcm, i + 1); } for (int m = 0; m < 2520; m += lcm) { ret += dp[0][e][0][mask][m]; } } } return ret; } int main() { pw[0] = 1; for (int i = 1; i < 19; ++i) { pw[i] = pw[i - 1] * 10; } i64 l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", solve(r) - solve(l - 1)); 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 | #include <cstdio> #include <cstring> using i64 = long long; i64 dp[2][2][2][1 << 9][2520], pw[19]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } i64 solve(i64 n) { if (n <= 0) return 0; int l = 1; while (n / pw[l]) ++l; memset(dp, 0, sizeof(dp)); dp[l & 1][1][1][0][0] = 1; for (int i = l; i >= 1; --i) { int u = i & 1, v = u ^ 1; memset(dp[v], 0, sizeof(dp[v])); int o = n / pw[i - 1] % 10; for (int e = 0; e < 4; ++e) { int vl = 1, vr = (e % 2) ? o : 9; if (e / 2) vl = 0; for (int d = vl; d <= vr; ++d) { int ne = (e % 2) && d == o, nz = (e / 2) && d == 0; for (int mask = 0; mask < (1 << 9); ++mask) { int n_mask = mask | (d ? (1 << (d - 1)) : 0); for (int m = 0; m < 2520; ++m) { if (!dp[u][e % 2][e / 2][mask][m]) continue; dp[v][ne][nz][n_mask][(m * 10 + d) % 2520] += dp[u][e % 2][e / 2][mask][m]; } } } } } i64 ret = 0; for (int e = 0; e < 2; ++e) { for (int mask = 1; mask < (1 << 9); ++mask) { int lcm = 1; for (int i = 0; i < 9; ++i) { if (mask >> i & 1) lcm = lcm * (i + 1) / gcd(lcm, i + 1); } for (int m = 0; m < 2520; m += lcm) { ret += dp[0][e][0][mask][m]; } } } return ret; } int main() { pw[0] = 1; for (int i = 1; i < 19; ++i) { pw[i] = pw[i - 1] * 10; } i64 l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", solve(r) - solve(l - 1)); return 0; } |