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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author dell
 */
public class row {
    static long k=  49;
    static long a = 0;
    static long b = Long.MAX_VALUE;
    static int wynik = 0;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {		
	
	row.readInput();
	
	if (Long.MAX_VALUE / row.k < 1459){
	    row.writeOutput();
	    return;
	}
	
	long start_min = (long) ((row.a+k-1) / k);

	start_min = Math.min(start_min, 1459);
	
	long start_max = row.b / k;
	start_max = Math.min(start_max, 1459);
	
	for(int i=(int)start_min ; i<=start_max;i++){
	    if(row.stprawdzLiczbe(i)){
		row.wynik++;
	    }
	}
	row.writeOutput();
    }
    
    public static boolean stprawdzLiczbe(int suma_cyfr){
	if (Long.MAX_VALUE / row.k < suma_cyfr){
	    return false;
	}
	
	long liczbaN = suma_cyfr * row.k;
//	if (liczbaN>row.b || liczbaN<row.a){
//	    return false;
//	}
	
	long cyfra;
	int sumaKwadratow = 0;
	while (liczbaN>0){
	    cyfra = liczbaN % 10;
	    sumaKwadratow+=cyfra*cyfra;
	    liczbaN/=10;
	}
	
	return sumaKwadratow==suma_cyfr;
    }
    
    private static void readInput(){
	
	try {
	    BufferedReader r1 = new BufferedReader(new InputStreamReader(System.in));
	    
	    	    
	    String we = r1.readLine();
	    String[] split = we.split(" ");
	    row.k = Long.parseLong(split[0]);	    
	    row.a = Long.parseLong(split[1]);
	    row.b = Long.parseLong(split[2]);
	} catch (IOException ex) {
	    Logger.getLogger(row.class.getName()).log(Level.SEVERE, null, ex);
	}
    }
    
    private static void writeOutput(){
	System.out.println(row.wynik);
    }
}