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
//package com.marcingum.pa.pot2015;

//import com.marcingum.pa.pot2012.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class row {

  public static void main(String[] args) throws IOException {
    BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tokenizer = new StringTokenizer(bufReader.readLine());
    long k = Long.parseLong(tokenizer.nextToken());
    long a = Long.parseLong(tokenizer.nextToken());
    long b = Long.parseLong(tokenizer.nextToken());
    System.out.println(compute(k, a, b));
    System.exit(0);
  }

  public static long compute(long k, long a, long b) {
    long wynik = 0;
    for (long n = ((long) Math.ceil(a / (double) k)) * k; n <= b; n+=k) {
      int cyfra;
      long reszta = n;
      int suma = 0;
      do {
        cyfra = (int) reszta % 10;
        reszta = reszta / 10;
        suma += cyfra * cyfra;
      } while (reszta > 0);
      if (suma * k == n) {
        //System.out.println(n + " suma:" + suma);
        wynik++;
      }
      if(k * liczbaCyfr(n) * 81 /*9^2*/ < n){
        break;
      }
    }
    return wynik;
  }

  private static long liczbaCyfr(long n) {
    return String.valueOf(n).length();
  }
}