//package row; import java.util.Scanner; public class row { class Solution { private final long k; private final long a; private final long b; private long MAGIC = 81 * 20; public Solution(long k, long a, long b) { this.k = k; this.a = a; this.b = b; } public void solve() { System.out.println(calc(b, k) - calc(a - 1, k)); } private int calc(long a, long k) { int result = 0; long _a = a / k; for(int i = 1; i <= Math.min(_a, MAGIC); ++i) { if(findSumOfDigitsSquares(k * i) == i) { ++result; } } return result; } private int findSumOfDigitsSquares(long n) { int sum = 0; while(n > 0) { sum += (n % 10) * (n % 10); n /= 10; } return sum; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long k = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); Solution solution = new row().new Solution(k, a, b); solution.solve(); } }
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 | //package row; import java.util.Scanner; public class row { class Solution { private final long k; private final long a; private final long b; private long MAGIC = 81 * 20; public Solution(long k, long a, long b) { this.k = k; this.a = a; this.b = b; } public void solve() { System.out.println(calc(b, k) - calc(a - 1, k)); } private int calc(long a, long k) { int result = 0; long _a = a / k; for(int i = 1; i <= Math.min(_a, MAGIC); ++i) { if(findSumOfDigitsSquares(k * i) == i) { ++result; } } return result; } private int findSumOfDigitsSquares(long n) { int sum = 0; while(n > 0) { sum += (n % 10) * (n % 10); n /= 10; } return sum; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long k = sc.nextLong(); long a = sc.nextLong(); long b = sc.nextLong(); Solution solution = new row().new Solution(k, a, b); solution.solve(); } } |