#include <iostream> #include <set> #include <map> bool is_potyczkowa(long long n) { if (n < 1) return false; for (long long x = n; x > 0; x /= 10) { long long digit = x % 10; if (digit == 0 || n % digit != 0) return false; } return true; } int set_of_digits(long long n, int omit=0) { int ret = 0; for (long long x = n; x > 0; x /= 10) { if (omit == 0) ret |= (1 << (x % 10)); else --omit; } return ret; } #define M 2520 long long D[M][19][1024]; void calc_D() { for (int l = 0; l < 19; ++l) { for (int a = 0; a < M; ++a) { for (int b = 0; b < 1024; ++b) { D[a][l][b] = 0; } } } for (long long i = 1; i < 10; ++i) { D[i % M][1][set_of_digits(i)] += 1; } long long ten_to_l = 10; for (int l = 2; l < 19; ++l) { for (int a = 0; a < M; ++a) { for (int b = 0; b < 1024; ++b) { for (int d = 0; d <= 9; ++d) { int new_ds = (b | (1 << d)); D[(a + ten_to_l * d) % M][l][new_ds] += D[a][l - 1][b]; } } } ten_to_l *= 10; } } long long count(int len, long long prefix) { if (len == 0) return is_potyczkowa(prefix); long long ret = 0; for (int l = 0; l <= len; ++l) { int suffix_ds = set_of_digits(prefix, l); for (int a = 0; a < M; ++a) { for (int b = 0; b < 1024; b += 2) { auto new_ds = suffix_ds | b; int na = (a + prefix) % M; bool pass = 1; for (int d = 0; d <= 9; ++d) { if (new_ds & (1 << d)) { if (d == 0 || na % d != 0) { pass = 0; break; } } } if (pass) { ret += D[a][l][b]; } } } } return ret; } long long count_to(long long n) { int len = 0; long long ret = is_potyczkowa(n); long long pow_10 = 1; for (long long x = n; x > 0; x /= 10) { for (int d = 0; d < x % 10; ++d) { ret += count(len, (x - x % 10 + d) * pow_10); } pow_10 *= 10; ++len; } return ret; } int main() { std::ios::sync_with_stdio(false); long long a, b; std::cin >> a >> b; calc_D(); std::cout << count_to(b) - count_to(a - 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include <iostream> #include <set> #include <map> bool is_potyczkowa(long long n) { if (n < 1) return false; for (long long x = n; x > 0; x /= 10) { long long digit = x % 10; if (digit == 0 || n % digit != 0) return false; } return true; } int set_of_digits(long long n, int omit=0) { int ret = 0; for (long long x = n; x > 0; x /= 10) { if (omit == 0) ret |= (1 << (x % 10)); else --omit; } return ret; } #define M 2520 long long D[M][19][1024]; void calc_D() { for (int l = 0; l < 19; ++l) { for (int a = 0; a < M; ++a) { for (int b = 0; b < 1024; ++b) { D[a][l][b] = 0; } } } for (long long i = 1; i < 10; ++i) { D[i % M][1][set_of_digits(i)] += 1; } long long ten_to_l = 10; for (int l = 2; l < 19; ++l) { for (int a = 0; a < M; ++a) { for (int b = 0; b < 1024; ++b) { for (int d = 0; d <= 9; ++d) { int new_ds = (b | (1 << d)); D[(a + ten_to_l * d) % M][l][new_ds] += D[a][l - 1][b]; } } } ten_to_l *= 10; } } long long count(int len, long long prefix) { if (len == 0) return is_potyczkowa(prefix); long long ret = 0; for (int l = 0; l <= len; ++l) { int suffix_ds = set_of_digits(prefix, l); for (int a = 0; a < M; ++a) { for (int b = 0; b < 1024; b += 2) { auto new_ds = suffix_ds | b; int na = (a + prefix) % M; bool pass = 1; for (int d = 0; d <= 9; ++d) { if (new_ds & (1 << d)) { if (d == 0 || na % d != 0) { pass = 0; break; } } } if (pass) { ret += D[a][l][b]; } } } } return ret; } long long count_to(long long n) { int len = 0; long long ret = is_potyczkowa(n); long long pow_10 = 1; for (long long x = n; x > 0; x /= 10) { for (int d = 0; d < x % 10; ++d) { ret += count(len, (x - x % 10 + d) * pow_10); } pow_10 *= 10; ++len; } return ret; } int main() { std::ios::sync_with_stdio(false); long long a, b; std::cin >> a >> b; calc_D(); std::cout << count_to(b) - count_to(a - 1); return 0; } |