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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <cassert>
#include <iostream>
#include <vector>

// LCM of all digits
static const uint64_t DIGIT_LCM = 2520;
static const uint8_t LIMIT_DIGITSET = 4 * 4 * 2 * 2;

uint64_t intpow(uint64_t x, uint64_t n) {
    if (n == 0) {
        return 1;
    }
    uint64_t result = intpow(x, n / 2);
    result *= result;
    if (n & 1) {
        result *= x;
    }
    return result;
}

struct digitset {
    uint8_t packed = 0;

    void add_digit(uint8_t digit) {
        switch (digit) {
        case 0:
            // Make invalid
            packed = 0xFF;
            break;

        case 1:
            break;

        case 2:
            add_two(1);
            break;

        case 3:
            add_three(1);
            break;

        case 4:
            add_two(2);
            break;

        case 5:
            packed |= 1 << 4;
            break;

        case 6:
            add_two(1);
            add_three(1);
            break;

        case 7:
            packed |= 1 << 5;
            break;

        case 8:
            add_two(3);
            break;

        case 9:
            add_three(2);
            break;

        default:
            break;
        }
    }

    void add_two(uint8_t level) {
        uint8_t two = std::max((uint8_t)(packed & 0x3), level);
        packed = (packed & ~0x3) | two;
    }

    void add_three(uint8_t level) {
        uint8_t three = std::max((uint8_t)(packed & 0xC), (uint8_t)(level << 2));
        packed = (packed & ~0xC) | three;
    }

    digitset united(digitset other) const {
        uint8_t two = std::max((uint8_t)(packed & 0x3), (uint8_t)(other.packed & 0x3));
        uint8_t three = std::max((uint8_t)(packed & 0xC), (uint8_t)(other.packed & 0xC));
        uint8_t five_seven = (packed | other.packed) & 0x30;
        return digitset{(uint8_t)(two | three | five_seven)};
    }

    unsigned int get_lcm() const {
        // TODO: we could use a lookup table for that
        unsigned int two = intpow(2, packed & 0x3);
        unsigned int three = intpow(3, (packed >> 2) & 0x3);
        unsigned int five = (packed & 0x10) ? 5 : 1;
        unsigned int seven = (packed & 0x20) ? 7 : 1;
        return two * three * five * seven;
    }

    bool is_valid() const {
        return (packed & 0xC) != 0xC;
    }

    bool is_divisible_by_ten() const {
        unsigned int two = packed & 0x3;
        unsigned int five = packed & 0x10;
        return two && five;
    }

    static digitset from_num(uint64_t num) {
        digitset ret{0};
        while (num > 0) {
            ret.add_digit(num % 10);
            num /= 10;
        }
        return ret;
    }
};

std::ostream& operator<<(std::ostream& os, const digitset& dset) {
    unsigned int two = dset.packed & 0x3;
    unsigned int three = (dset.packed >> 2) & 0x3;
    unsigned int five = (dset.packed & 0x10) ? 1 : 0;
    unsigned int seven = (dset.packed & 0x20) ? 1 : 0;
    os << "{[2]:" << two << " [3]:" << three << " [5]:" << five << " [7]:" << seven << " lcm:" << dset.get_lcm() << "}";
    return os;
}

uint64_t data[20][LIMIT_DIGITSET][DIGIT_LCM] = { 0 };

uint64_t numlen(uint64_t num) {
    uint64_t len = 0;
    do {
        num /= 10;
        len++;
    } while (num > 0);
    return len;
}

std::vector<uint8_t> to_digits(uint64_t num) {
    std::vector<uint8_t> ret;
    do {
        ret.push_back(num % 10);
        num /= 10;
    } while (num > 0);
    return ret;
}

void precompute_up_to_level(const uint64_t max_level) {
    // Prepare the zeroth level
    data[0][0][0] = 1; // Others are zeros

    // Other levels
    uint64_t tenpow = 1;
    for (uint64_t level = 1; level <= max_level; level++) {
        for (uint64_t d = 1; d <= 9; d++) {
            const uint64_t offset = (d * tenpow) % DIGIT_LCM;

            for (uint8_t raw_source_dset = 0; raw_source_dset < LIMIT_DIGITSET; raw_source_dset++) {
                digitset dset{raw_source_dset};
                if (!dset.is_valid()) {
                    continue;
                }

                dset.add_digit(d);
                const uint8_t raw_dest_dset = dset.packed;

                for (uint64_t i = 0; i < DIGIT_LCM - offset; i++) {
                    data[level][raw_dest_dset][i + offset] += data[level - 1][raw_source_dset][i];
                }
                for (uint64_t i = DIGIT_LCM - offset; i < DIGIT_LCM; i++) {
                    data[level][raw_dest_dset][i - DIGIT_LCM + offset] += data[level - 1][raw_source_dset][i];
                }
            }
        }
        tenpow *= 10;
    }
}

// Computes how many 
uint64_t count_with_offset(uint64_t level, uint64_t offset, digitset base_dset) {
    // std::cout << "count_with_offset: " << level << " " << offset << " " << base_dset << std::endl;

    if (base_dset.is_divisible_by_ten()) {
        // std::cout << "  divisible by ten, skipping" << std::endl;
        return 0;
    }

    uint64_t count = 0;
    offset %= DIGIT_LCM;

    for (uint8_t raw_dset = 0; raw_dset < LIMIT_DIGITSET; raw_dset++) {
        digitset dset = digitset{raw_dset};
        if (!dset.is_valid()) {
            continue;
        }

        // std::cout << "  united: " << dset << std::endl;

        dset = dset.united(base_dset);

        if (dset.is_divisible_by_ten()) {
            continue;
        }

        unsigned int lcm = dset.get_lcm();

        for (unsigned int i = (DIGIT_LCM - offset) % lcm; i < DIGIT_LCM; i += lcm) {
            // if (data[level][raw_dset][i] > 0) {
            //     std::cout << "    adding [" << i << "] = " << data[level][raw_dset][i] << std::endl;
            // }
            count += data[level][raw_dset][i];
        }
    }

    // std::cout << "  return " << count << std::endl;

    return count;
}

uint64_t count_in_range(uint64_t begin, uint64_t end) {
    if (begin >= end) {
        return 0;
    }

    auto begin_digits = to_digits(begin);
    auto end_digits = to_digits(end);

    // Make both representations have the same size
    begin_digits.resize(end_digits.size(), 0);

    // Compute length of the prefix without the common part
    int uncommon = end_digits.size();
    // No need to check uncommon > 0, because both vectors are
    // guaranteed to be different
    while (begin_digits[uncommon - 1] == end_digits[uncommon - 1]) {
        uncommon--;
    }

    uint64_t base = begin;
    uint64_t count = 0;
    uint64_t tenpow = 1;

    if (begin_digits[0] == 0) {
        begin_digits[0]++;
        base++;
    }

    for (int i = 0; i < uncommon - 1; i++) {
        for (uint8_t d = begin_digits[i]; d <= 9; d++) {
            // std::cout << "  base: " << base << ", end: " << end << std::endl;
            digitset dset = digitset::from_num(base / tenpow);
            if (dset.is_valid()) {
                count += count_with_offset(i, base, dset);
            }
            base += tenpow;
        }

        tenpow *= 10;
        begin_digits[i + 1]++;
    }

    for (uint8_t d = std::max(begin_digits[uncommon - 1], (uint8_t)1); d < end_digits[uncommon - 1]; d++) {
        // std::cout << "  base: " << base << ", end: " << end << std::endl;
        digitset dset = digitset::from_num(base / tenpow);
        if (dset.is_valid()) {
            count += count_with_offset(uncommon - 1, base, dset);
        }
        base += tenpow;
    }

    for (int i = uncommon - 2; i >= 0; i--) {
        tenpow /= 10;
        for (uint8_t d = 0; d < end_digits[i]; d++) {
            // std::cout << "  base: " << base << ", end: " << end << std::endl;
            digitset dset = digitset::from_num(base / tenpow);
            if (dset.is_valid()) {
                count += count_with_offset(i, base, dset);
            }
            base += tenpow;
        }
    }

    // std::cout << "  base: " << base << ", end: " << end << std::endl;

    assert(base == end);

    return count;
}

int main() {
    std::ios::sync_with_stdio(false);

    uint64_t l, r;
    std::cin >> l >> r;

    precompute_up_to_level(numlen(r));
    const uint64_t count = count_in_range(l, r + 1);
    std::cout << count << std::endl;

    return 0;
}