#include <algorithm> #include <array> #include <iostream> #include <iterator> #include <numeric> using namespace std; namespace { using std::lcm; template<typename T, typename... Ts> constexpr auto lcm(T x, Ts... xs) { return lcm(x, lcm(xs...)); } template<int X> constexpr auto divisor_count() { int res = 0; int d = 1; while (d < X / d) { if (X % d == 0) res += 2; ++d; } if (X % d == 0 && d == X / d) ++res; return res; } template <int X> constexpr array<int, divisor_count<X>()> divisors() { array<int, divisor_count<X>()> res{}; int i = 0; int j = size(res) - 1; int d = 1; while (d < X / d) { if (X % d == 0) { res[i++] = d; res[j--] = X / d; } ++d; } if (X % d == 0 && d == X / d) res[i++] = d; return res; } template<int N, typename T> constexpr array<T, N> first_powers(T p) { array<T, N> res{}; res[0] = 1; for (int i = 1; i < N; ++i) { res[i] = p * res[i - 1]; } return res; } using ull = unsigned long long; constexpr auto mod = lcm(1, 2, 3, 4, 5, 6, 7, 8, 9); constexpr auto max_len = 18; constexpr auto pow10 = first_powers<max_len + 1>(10ULL); class State { private: constexpr static auto divs = divisors<mod>(); int r = 0; int m = 1; State(int r_, int m_): r{r_}, m{m_} { } public: State() = default; constexpr static int max = mod * size(divs); static State decode(int s) { return State{s % mod, divs[s / mod]}; } State with(int d) const { return State{(10 * r + d) % mod, lcm(m, d)}; } int encode() const { return r + mod * (find(begin(divs), end(divs), m) - begin(divs)); } int count() const { if (r % m != 0) return 0; return 1; } }; ull count_len(int l, State s={}) { if (l == 0) return s.count(); static ull memo[max_len][State::max]; if (memo[l - 1][s.encode()] > 0) return memo[l - 1][s.encode()] - 1; ull res = 0; for (int d = 1; d <= 9; ++d) { res += count_len(l - 1, s.with(d)); } memo[l - 1][s.encode()] = res + 1; return res; } int find_len(ull x) { int l = 1; while (x >= 10) { x /= 10; ++l; } return l; } ull count_le(ull x, int l, State s={}) { if (l == 0) return s.count(); int f = x / pow10[l - 1]; if (f == 0) return 0; auto res = count_le(x - f * pow10[l - 1], l - 1, s.with(f)); for (int d = 1; d < f; ++d) { res += count_len(l - 1, s.with(d)); } return res; } ull solve1(ull x) { if (x < 1) return 0; if (x % 10 == 0) --x; auto l = find_len(x); ull res = 0; for (int q = 1; q < l; ++q) { res += count_len(q); } res += count_le(x, l); return res; } auto solve(ull l, ull r) { return solve1(r) - solve1(l - 1); } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); ull l, r; cin >> l >> r; cout << solve(l, r) << 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #include <algorithm> #include <array> #include <iostream> #include <iterator> #include <numeric> using namespace std; namespace { using std::lcm; template<typename T, typename... Ts> constexpr auto lcm(T x, Ts... xs) { return lcm(x, lcm(xs...)); } template<int X> constexpr auto divisor_count() { int res = 0; int d = 1; while (d < X / d) { if (X % d == 0) res += 2; ++d; } if (X % d == 0 && d == X / d) ++res; return res; } template <int X> constexpr array<int, divisor_count<X>()> divisors() { array<int, divisor_count<X>()> res{}; int i = 0; int j = size(res) - 1; int d = 1; while (d < X / d) { if (X % d == 0) { res[i++] = d; res[j--] = X / d; } ++d; } if (X % d == 0 && d == X / d) res[i++] = d; return res; } template<int N, typename T> constexpr array<T, N> first_powers(T p) { array<T, N> res{}; res[0] = 1; for (int i = 1; i < N; ++i) { res[i] = p * res[i - 1]; } return res; } using ull = unsigned long long; constexpr auto mod = lcm(1, 2, 3, 4, 5, 6, 7, 8, 9); constexpr auto max_len = 18; constexpr auto pow10 = first_powers<max_len + 1>(10ULL); class State { private: constexpr static auto divs = divisors<mod>(); int r = 0; int m = 1; State(int r_, int m_): r{r_}, m{m_} { } public: State() = default; constexpr static int max = mod * size(divs); static State decode(int s) { return State{s % mod, divs[s / mod]}; } State with(int d) const { return State{(10 * r + d) % mod, lcm(m, d)}; } int encode() const { return r + mod * (find(begin(divs), end(divs), m) - begin(divs)); } int count() const { if (r % m != 0) return 0; return 1; } }; ull count_len(int l, State s={}) { if (l == 0) return s.count(); static ull memo[max_len][State::max]; if (memo[l - 1][s.encode()] > 0) return memo[l - 1][s.encode()] - 1; ull res = 0; for (int d = 1; d <= 9; ++d) { res += count_len(l - 1, s.with(d)); } memo[l - 1][s.encode()] = res + 1; return res; } int find_len(ull x) { int l = 1; while (x >= 10) { x /= 10; ++l; } return l; } ull count_le(ull x, int l, State s={}) { if (l == 0) return s.count(); int f = x / pow10[l - 1]; if (f == 0) return 0; auto res = count_le(x - f * pow10[l - 1], l - 1, s.with(f)); for (int d = 1; d < f; ++d) { res += count_len(l - 1, s.with(d)); } return res; } ull solve1(ull x) { if (x < 1) return 0; if (x % 10 == 0) --x; auto l = find_len(x); ull res = 0; for (int q = 1; q < l; ++q) { res += count_len(q); } res += count_le(x, l); return res; } auto solve(ull l, ull r) { return solve1(r) - solve1(l - 1); } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); ull l, r; cin >> l >> r; cout << solve(l, r) << endl; return 0; } |