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
#include<bits/stdc++.h>

using namespace std;

long long k, a, b;
long long maks = 1000000000000000000;

long long wyn;

vector<long long>v;

int main(){
	scanf("%lld%lld%lld", &k, &a, &b);
	for(int i = 1; i <= min(1200ll, (maks + k - 1) / k); i++){
		
		long long pom = 0ll; //f(n)
		long long q = i * k; //potencjalne n
		while(q > 0){
			long long w = q % 10;
			q /= 10;
			pom += w * w;
		}
		if(pom * k == i * k){
			v.push_back(i * k);
		}
		
	}
	for(int i = 0; i < v.size(); i++){
		if(v[i] >= a && v[i] <= b){
			wyn++;
		}
		else if(v[i] > b){break;}
	}
	//cout << wyn << endl;
	printf("%lld", wyn);
	return 0;
}