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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

int main(int argc, char const *argv[]) {
  unsigned long long int from, to, result = 0, i, icp, m, m2 = 0, mem = 0, mem2 = 0, incrCount = 0, incrCount5 = 0;
  int m3, incr = 1;
  bool is;

  if (!scanf("%llu %llu", &from, &to)) return 1;

  for (i = from; i <= to; i = i + incr) {
    if (incrCount > 0) {
      incrCount -= 2;
      if (incrCount > 2) {
        incr = 2;
      } else {
        incrCount = 0;
        incr = 1;
      }
    } else if (incrCount5 > 0) {
      incrCount5 -= 10;
      if (incrCount5 >= 10) {
        incr = 10;
      } else {
        incrCount5 = 0;
        incr = 5;
      }
    } else {
      incr = 1;
    }

    icp = i;
    is = true;
    mem = 0;

    do {
      m2 = icp % 10;

      if (m2 == 0) {
        m3 = -1;
        do {
          icp = icp / 10;
          m2 = icp % 10;
          ++m3;
        } while (m2 == 0);

        if (incrCount == 0 && incrCount5 == 0 && icp % 2 == 0) {
          incrCount = pow(10, m3 + 1);
          if (incrCount == 10) {
            incrCount += 2;
          }
          incr = 2;
        } else if (incrCount == 0 && incrCount5 == 0 && icp % 5 == 0) {
          incrCount5 = pow(10, m3 + 1);
          i -= 5;
          incr = 10;
        }

        if (m3 > 0) {
          m2 = 10;
          do {
            i += m2;
            if (incrCount > 0) {
              incrCount -= m2;
            }
            if (incrCount5 > 0) {
              incrCount5 -= m2;
            }
            m2 *= 10;
          } while (--m3 > 0);
        }
        is = false;
        break;
      }
      if (m2 == 1 || m2 == mem) {
        icp = icp / 10;
        continue;
      }
      mem = m2;

      m = i % m2;
      if (m > 0) {
        is = false;
        break;
      }

      icp = icp / 10;
    } while (icp > 0);

    if (is) {
      ++result;
    }

    // if (i % 100000 == 0) {
    //   printf("%llu - %llu\n", i, result - m3);
    //   m3 = result;
    // }
  }
  printf("%llu", result);

  return 0;
}