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
#include <iostream> 
#include <math.h>
#include <vector>

void inc_inst(int* inst, int amount) {
  int n = amount;

  for(int i = 0; n != 0; i++) {
    inst[i] += n % 10;
    n /= 10;

    if(inst[i] > 9) {
      inst[i] %= 10;
      n += 1;
    }
  }
}

int step(std::vector<int>& inst, int inst_size, int* digits) {
  int n        = 0; 
  int carry    = 1;
  int dec_mult = 1;
  int i        = 0;

  for(; i < inst_size && carry == 1; i++) {
    int old_d = inst[i];

    digits[inst[i] - 1] --; // dec the old digit        
    
    // Increment the inst
    inst[i] += carry;
    carry    = inst[i] / 10;
    inst[i]  = inst[i] % 10;

    if (inst[i] == 0) {
      inst[i] = 1;
    }

    digits[inst[i] - 1] ++; // inc the new digit

    n        += dec_mult * (inst[i] - old_d);
    dec_mult *= 10;
  }

  return n;
} 

void formalize(std::vector<int>& inst, int inst_len, int* digits, int n_) {
  int n = n_;

  for(int i = 0; i < inst_len; i++) {
    int d   = n % 10;
    inst[i] = d;
    if (d != 0) digits[d-1] += 1;

    n /= 10;
  }
}

int main() {
  int l, r;
  std::cin >> l >> r;

  int digits[9] = {0};
  int count     = 0;
  int n_len     = (double)(std::log10(r)) + 1;
  std::vector<int> num(n_len, 0);

  formalize(num, n_len, digits, l);

  while( l < r + 1 ) {
    bool all_divide = true;
    for(int i = 0; i < 9 && all_divide; i++) {
      if(digits[i] == 0) continue;

      all_divide = ( l % (i+1) == 0 );
    }

    if(all_divide) count ++;

    l += step(num, n_len, digits);
  }

  std::cout << count;
  return 0;
}