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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>
#include <sstream>
 
using namespace std;
 
int NWD(unsigned long long a, unsigned long long b){
	
    while(a!=b)
       if(a>b)
           a-=b;
       else
           b-=a;
           
    return a; 
}
 
int main()
{
    unsigned long long k, a, b, n, w, wynik=0;
    string si;
 
    cin >> k >> a >> b;
    
    while(a<=b){
    	
    	if(NWD(k,a) == k) break;	
    	
    	a++;
    	
    }
    
    for(unsigned long long i=a;i<b;i+=k){
    	
    	w=0;

		ostringstream ss;
		ss << i;
		si = ss.str();
		
		for(int j=0;j<si.length();j++){
			
			if(si[j]=='0') w+=0;
			else if(si[j]=='1') w+=1;
			else if(si[j]=='2') w+=4;
			else if(si[j]=='3') w+=9;
			else if(si[j]=='4') w+=16;
			else if(si[j]=='5') w+=25;
			else if(si[j]=='6') w+=36;
			else if(si[j]=='7') w+=49;
			else if(si[j]=='8') w+=64;
			else if(si[j]=='9') w+=81;
			
			if(w*k>i) break;
			
		}
		
		if(i==w*k) wynik++;
    	
    }
    
    cout << wynik << endl;

    return 0;
}