import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.StringTokenizer; public class row { static long getLong(StringTokenizer t) { return Long.parseLong(t.nextToken()); } static long now() { return System.currentTimeMillis(); } static int sumOfSquaredDigits(BigInteger a) { int res = 0; BigInteger ten = BigInteger.valueOf(10); while (a.compareTo(BigInteger.ZERO) > 0) { int digit = a.mod(ten).intValue(); res += digit*digit; a = a.divide(ten); } return res; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer t; long k, a, b; int res = 0; t = new StringTokenizer(br.readLine()); k = getLong(t); a = getLong(t); b = getLong(t); // System.out.println("got: " + k + ", " + a + ", " + b); BigInteger kk, aa, bb; kk = BigInteger.valueOf(k); aa = BigInteger.valueOf(a); bb = BigInteger.valueOf(b); for (int i=0; i<=2000; i++) { BigInteger val = kk.multiply(BigInteger.valueOf(i)); if ((val.compareTo(aa) >= 0) && (val.compareTo(bb) <= 0)) { int sosd = sumOfSquaredDigits(val); // System.out.println("sosd: " + sosd); if (val.compareTo(kk.multiply(BigInteger.valueOf(sosd))) == 0) { res++; // System.out.println(val); } } } System.out.println(res); } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.StringTokenizer; public class row { static long getLong(StringTokenizer t) { return Long.parseLong(t.nextToken()); } static long now() { return System.currentTimeMillis(); } static int sumOfSquaredDigits(BigInteger a) { int res = 0; BigInteger ten = BigInteger.valueOf(10); while (a.compareTo(BigInteger.ZERO) > 0) { int digit = a.mod(ten).intValue(); res += digit*digit; a = a.divide(ten); } return res; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer t; long k, a, b; int res = 0; t = new StringTokenizer(br.readLine()); k = getLong(t); a = getLong(t); b = getLong(t); // System.out.println("got: " + k + ", " + a + ", " + b); BigInteger kk, aa, bb; kk = BigInteger.valueOf(k); aa = BigInteger.valueOf(a); bb = BigInteger.valueOf(b); for (int i=0; i<=2000; i++) { BigInteger val = kk.multiply(BigInteger.valueOf(i)); if ((val.compareTo(aa) >= 0) && (val.compareTo(bb) <= 0)) { int sosd = sumOfSquaredDigits(val); // System.out.println("sosd: " + sosd); if (val.compareTo(kk.multiply(BigInteger.valueOf(sosd))) == 0) { res++; // System.out.println(val); } } } System.out.println(res); } } |