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

bool test(unsigned long long k, unsigned long long i)
{
	unsigned long long n = i;
	unsigned long long sum = 0;
	while (i != 0) {
		unsigned long long dig = i % 10;
		sum += dig * dig; 
		i /= 10;
	}
	return (k*sum == n);
}

int main()
{
	unsigned long long k;
	unsigned long long a;
	unsigned long long b;
	scanf("%llu %llu %llu", &k, &a, &b);
	unsigned long long solutions = 0;
	for (unsigned long long i = k+((a/k)*k); i < b; i += k) {
		if (test(k, i)) {
			solutions ++;
		}
	}
	printf("%llu\n", solutions);
}