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
#include <stdio.h>
typedef unsigned int I;
typedef unsigned long long L;

static const I MAXFN = 18*9*9;

static inline I f(L n) {
    I ret = 0;
    while (n) {
        I digit = n % 10;
        ret += digit * digit;
        n /= 10;
    }
    return ret;
}

static L calc(L k, L a, L b) {
    L ret = 0;
    for (I fn=1; fn<=MAXFN; ++fn) {
        L n = fn*k;
        if (n < (fn-1)*k) // overflow
            break;
        if (n >= a && n <= b && k*f(n) == n) {
            ++ret;
        }
    }
    return ret;
}

int main() {
  L k, a, b;
  scanf("%llu %llu %llu\n", &k, &a, &b);
  printf("%llu\n", calc(k, a, b));
  return 0;
}