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
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>

// Maximum value of f for arguments <= 10e18
static const uint64_t MAX_F = 81 * 17;

inline uint64_t f(uint64_t n)
{
	int ret = 0;
	while (n > 0)
	{
		int digit = int(n % 10);
		ret += digit * digit;
		n /= 10;
	}

	return uint64_t(ret);
}

int solve(uint64_t k, uint64_t a, uint64_t b)
{
	const uint64_t start = (a + k - 1) / k;
	const uint64_t end = std::min(b / k, MAX_F) + 1;
	int count = 0;

	for (uint64_t i = start; i < end; i++)
	{
		uint64_t n = i * k;
		if (k * f(n) == n)
			count++;
	}

	return count;
}

int main()
{
	uint64_t k, a, b;
	scanf("%lld %lld %lld", &k, &a, &b);

	int result = solve(k, a, b);
	printf("%d\n", result);

	return 0;
}