結果
問題 | No.6 使いものにならないハッシュ |
ユーザー |
![]() |
提出日時 | 2015-02-02 23:24:54 |
言語 | Java (openjdk 23) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,292 bytes |
コンパイル時間 | 2,397 ms |
コンパイル使用メモリ | 78,000 KB |
実行使用メモリ | 40,988 KB |
最終ジャッジ日時 | 2024-09-16 16:23:46 |
合計ジャッジ時間 | 6,714 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 RE * 1 |
ソースコード
import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.TreeSet; public class Main { int hash(int k) { if (k < 10) return k; int sum = 0; while (0 < k) { sum += k % 10; k /= 10; } return hash(sum); } void run() { MyScanner sc = new MyScanner(); int max = 200001; boolean[] prime = new boolean[max]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 0; i * i <= max; i++) { if (prime[i]) { for (int j = i * i; j < max; j += i) { prime[j] = false; } } } int n = sc.nextInt(); int m = sc.nextInt(); int count = 0; for (int i = n; i <= m; i++) { if (prime[i]) count++; } int[] list = new int[count]; int[] hash = new int[count]; int id = 0; for (int i = n; i <= m; i++) { if (prime[i]) { list[id] = i; hash[id] = hash(i); id++; } } int left = 0, right = 1; TreeSet<Integer> set = new TreeSet<Integer>(); int maxLength = 0; int maxNumber = 0; set.add(hash[left]); while (left < id) { if (id <= right || set.contains(hash[right])) { set.remove(hash[left++]); } else { set.add(hash[right++]); } if (maxLength <= right - left) { maxLength = right - left; maxNumber = list[left]; } } System.out.println(maxNumber); } public static void main(String[] args) { new Main().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------" + '\n'); } class MyScanner { int read() { try { return System.in.read(); } catch (IOException e) { throw new InputMismatchException(); } } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }