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
48
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

long long k, a, b;

int getSum(long long n)
{
	int sum = 0;
	while (n)
	{
		sum += (n % 10) * (n % 10);
		n /= 10;
	}
	return sum;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin >> k >> a >> b;
	int total = 0;

	long long limits[20];

	for (int i = 0; i < 20; i++)
	{
		limits[i] = 81 * i;
		limits[i] *= k;
	}
	limits[0] = 1;

	long long start = min((long long)ceil((double)a / k) * k, b);
	

	int maks = ceil(log10(b));
	b = max(min(b, limits[maks]),start);

	for (long long i = start; i <= b; i += k)
	{
		if (getSum(i) * k == i)
			total++;
	}
	cout << total << endl;
	return 0;
}