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
#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
  __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                   __gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1);    // Out: 0.
// *s.find_by_order(1);  // Out: 2.

using ld = long double;
using ll = long long;

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

constexpr int nax = 50;
constexpr int mod = 5 * 7 * 8 * 9;

constexpr int pot2[] = {1, 2, 4, 8};
constexpr int pot3[] = {1, 3, 9};
constexpr int pot5[] = {1, 5};
constexpr int pot7[] = {1, 7};

constexpr int MaxPot2 = sizeof(pot2) / sizeof(pot2[0]);
constexpr int MaxPot3 = sizeof(pot3) / sizeof(pot3[0]);
constexpr int MaxPot5 = sizeof(pot5) / sizeof(pot5[0]);
constexpr int MaxPot7 = sizeof(pot7) / sizeof(pot7[0]);

int potega[10][4];

int Potega(int cyfra, int pot) {
  int wynik = 0;
  while (cyfra % pot == 0) {
    wynik++;
    cyfra /= pot;
  }
  return wynik;
}

void WypelnijPotega() {
  for (int cyfra = 1; cyfra < 10; cyfra++) {
    potega[cyfra][0] = Potega(cyfra, 2);
    potega[cyfra][1] = Potega(cyfra, 3);
    potega[cyfra][2] = Potega(cyfra, 5);
    potega[cyfra][3] = Potega(cyfra, 7);
  }
}

int D;
int digits[nax];
ll dp[nax][mod][MaxPot2][MaxPot3][MaxPot5][MaxPot7];

void FillDigits(ll ogr) {
  D = 0;
  while (ogr > 0) {
    digits[D++] = ogr % 10;
    ogr /= 10;
  }
}

void Wyzeruj() {
  for (int d = 0; d <= D; d++) {
    for (int m = 0; m < mod; m++) {
      for (int p2 = 0; p2 < MaxPot2; p2++) {
        for (int p3 = 0; p3 < MaxPot3; p3++) {
          for (int p5 = 0; p5 < MaxPot5; p5++) {
            for (int p7 = 0; p7 < MaxPot7; p7++) {
              dp[d][m][p2][p3][p5][p7] = 0;
            }
          }
        }
      }
    }
  }
}

tuple<int, int, int, int, int> DodajCyfre(
    int m, int p2, int p3, int p5, int p7, int cyfra) {
  return make_tuple(
      (m * 10 + cyfra) % mod,
      max(p2, potega[cyfra][0]),
      max(p3, potega[cyfra][1]),
      max(p5, potega[cyfra][2]),
      max(p7, potega[cyfra][3]));
}

ll Sprawdz(ll val, int m, int p2, int p3, int p5, int p7) {
  if (m % pot2[p2] == 0 and
      m % pot3[p3] == 0 and
      m % pot5[p5] == 0 and
      m % pot7[p7] == 0) {
    return val;
  }
  return 0;
}

ll Licz(ll ogr) {
  debug() << imie(ogr);
  FillDigits(ogr);
  Wyzeruj();

  ll zgadza_sie = 1;
  int zg_m = 0, zg_p2 = 0, zg_p3 = 0, zg_p5 = 0, zg_p7 = 0;
  for (int d = D; d > 0; d--) {
    debug() << imie(d) imie(digits[d - 1]);
    for (int cyfra = 1; cyfra < digits[d - 1]; cyfra++) {
      const auto& [m, p2, p3, p5, p7] =
          DodajCyfre(zg_m, zg_p2, zg_p3, zg_p5, zg_p7, cyfra);
      debug() << "[" imie(d-1) imie(m) imie(p2) imie(p3) imie(p5) imie (p7) "] += " imie(zgadza_sie);
      dp[d - 1][m][p2][p3][p5][p7] += zgadza_sie;
    }
    if (digits[d - 1] == 0) {
      zgadza_sie = 0;
    } else {
      tie(zg_m, zg_p2, zg_p3, zg_p5, zg_p7) =
          DodajCyfre(zg_m, zg_p2, zg_p3, zg_p5, zg_p7, digits[d - 1]);
    }
    for (int m = 0; m < mod; m++) {
      for (int p2 = 0; p2 < MaxPot2; p2++) {
        for (int p3 = 0; p3 < MaxPot3; p3++) {
          for (int p5 = 0; p5 < MaxPot5; p5++) {
            for (int p7 = 0; p7 < MaxPot7; p7++) {
              for (int cyfra = 1; cyfra < 10; cyfra++) {
                const auto& [n_m, n_p2, n_p3, n_p5, n_p7] =
                    DodajCyfre(m, p2, p3, p5, p7, cyfra);
                dp[d - 1][n_m][n_p2][n_p3][n_p5][n_p7] +=
                    dp[d][m][p2][p3][p5][p7];
              }
            }
          }
        }
      }
    }
    if (d < D) {
      for (int cyfra = 1; cyfra < 10; cyfra++) {
        const auto& [m, p2, p3, p5, p7] = DodajCyfre(0, 0, 0, 0, 0, cyfra);
        dp[d - 1][m][p2][p3][p5][p7]++;
      }
    }
  }
  ll wynik = 0;
  for (int m = 0; m < mod; m++) {
    for (int p2 = 0; p2 < MaxPot2; p2++) {
      for (int p3 = 0; p3 < MaxPot3; p3++) {
        for (int p5 = 0; p5 < MaxPot5; p5++) {
          for (int p7 = 0; p7 < MaxPot7; p7++) {
            wynik += Sprawdz(dp[0][m][p2][p3][p5][p7], m, p2, p3, p5, p7);
          }
        }
      }
    }
  }
  debug() << imie(wynik);
  return wynik;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  WypelnijPotega();

  ll l, r;
  cin >> l >> r;
  cout << Licz(r + 1) - Licz(l) << endl;
  return 0;
}