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
#include <iostream>
#include <cstdint>

using namespace std;

int64_t f(int64_t n) {
  int64_t sum = 0;
  while (n > 0) {
    int64_t q = n % 10;
    sum += q * q;
    n /= 10;
  }
  return sum;
}

int main() {
  int64_t k, a, b;
  cin >> k >> a >> b;
  int64_t count = 0;
  for (int64_t fn = 1; fn <= 20 * 9 * 9; ++fn) {
    int64_t n = k * fn;
    if (n / k != fn) {
      // cerr << "overflow for " << fn << endl;
      continue;
    }
    if (f(n) == fn && a <= n && n <= b) {
      ++count;
      // cout << "fn:" << fn << " n:" << n << " f(n):" << f(n) << endl;
    }
  }
  cout << count << endl;
}