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
#include <cstdio>
#define U unsigned
#define ULL U long long

U gcd (U a, U b)
{
  while (b>0)
  {
    a %= b;
    a ^= b;
    b ^= a;
    a ^= b;
  }
  return a;
}

U lcm (U a, U b)
{
  return a / gcd (a, b) * b;
}

U dlcm (ULL a)
{
  U l;
  for (l=1; a>0; a/=10) l = lcm (l, a%10);
  return l;
}

#define M 2520
#define N 18
#define D 48

ULL cc[M][N+1][D+1];
U dd[M+1];
U mm[N+1];

ULL count (U m, U n, U d)
{
  ULL r = cc[m][n][dd[d]];
  if (r) return r-1;
  if (d%10==0) r = 0;
  else if (n==0) r = m%d==0;
  else for (U i=1; i<10; i++) r += count ((m+i*mm[n-1])%M, n-1, lcm(d,i));
  cc[m][n][dd[d]] = r+1;
  return r;
}

int main ()
{
  for (U a=1; a<10; a*=2)
  for (U b=1; b<10; b*=3)
  for (U c=1; c<10; c*=5)
  for (U d=1; d<10; d*=7)
  dd[lcm(lcm(a,b),lcm(c,d))] = 1;
  for (U m=0, d=0; m<=M; m++) if (dd[m]) dd[m] = ++d;
  for (U n=0, m=1; n<=N; n++, m*=10, m%=M) mm[n] = m;

  ULL l, r, p, q, w=0;
  scanf ("%llu%llu", &l, &r);
  for (p=l; p<=r; p+=q)
  {
    U n=0;
    for (q=1; p%(q*10)==0 && p+q*10-1<=r; q*=10, n++);
    w += count (p%M, n, dlcm(p/q));
  }
  printf ("%llu\n", w);
  return 0;
}