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
#include <cstdio>

unsigned long long min(unsigned long long a, unsigned long long b) {return a<=b?a:b;}
int main() {
  
  unsigned long long k, a, b;
  scanf("%llu %llu %llu", &k, &a, &b);
  a += (k - a%k) % k;
  //printf("a: %llu\n", a);
  unsigned long long max_n = 9;
  for (int i = 1; i <= 18; i++) {
    if (k < max_n / (81 * i)) {
      break;
    }
    max_n = (max_n + 1) * 10 - 1;
  }
  //printf("max_n: %llu\n", max_n);
  b = min(b, max_n);
  //printf("b: %llu\n", b);
  long long result = 0;
  for (unsigned long long i = a; i <= b; i+=k) {
    int sum_digits_sq = 0;
    unsigned long long j = i;
    while (j > 0) {
      sum_digits_sq += (j%10) * (j%10);
      j /= 10;
    }
    if (i == k*sum_digits_sq) {
      //printf("%llu\n", i);
      result++;
    }
  }
  printf("%llu\n", result);
}