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
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by micbud on 23.09.15.
 */
public class row {
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String[] values = input.readLine().split(" ");
        Long k = Long.parseLong(values[0]);
        Long a = Long.parseLong(values[1]);
        Long b = Long.parseLong(values[2]);
        System.out.println(solve(k, a, b));
    }

    private static int solve(long k, long a, long b) {
        Fn fn = new Fn(a);
        int result = 0;
        while(a<=b) {
            if(check(a, k, fn)) {
                ++result;
            }
            ++a;
            fn.change(a);
        }
        return result;
    }

    private static boolean check(long a, long k, Fn fn) {
        if (a < fn.fn) {
            return false;
        }

        if (a == fn.fn) {
            return k == 1;
        }

        long t = a/fn.fn;
        if (t == k) {
            return t * fn.fn == a;
        }

        return false;
    }


    private static class Fn {
        public int fn = 0;
        public int[] tab = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

        public Fn(long n) {
            int i = 0;
            long r;
            while (n>0) {
                r = n % 10;
                tab[i] = (int) r;
                fn += r * r;
                n /= 10;
                ++i;
            }
        }

        public void change(long n) {
            boolean stop = false;
            int i = 0;
            long r;
            while (!stop && n >0) {
                r = n % 10;
                if (tab[i] != r) {
                    fn -= tab[i] * tab[i];
                    tab[i] = (int) r;
                    fn += r * r;
                    n /= 10;
                    ++i;
                }
                else {
                    stop = true;
                }
            }
        }
    }
}