結果
問題 | No.6 使いものにならないハッシュ |
ユーザー |
![]() |
提出日時 | 2022-08-18 18:26:41 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 113 ms / 5,000 ms |
コード長 | 2,161 bytes |
コンパイル時間 | 2,266 ms |
コンパイル使用メモリ | 78,636 KB |
実行使用メモリ | 40,576 KB |
最終ジャッジ日時 | 2024-10-06 12:19:20 |
合計ジャッジ時間 | 5,867 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int k = sc.nextInt(); int n = sc.nextInt(); HashSet<Integer> hashes = new HashSet<>(); ArrayDeque<Integer> deq = new ArrayDeque<>(); boolean[] isNotPrimes = new boolean[n + 1]; int max = 0; int ans = 0; for (int i = 2; i <= n; i++) { if (isNotPrimes[i]) { continue; } for (int j = 2; j * i <= n; j++) { isNotPrimes[j * i] = true; } if (i < k) { continue; } int h = getHash(i); if (hashes.contains(h)) { int x; while (h != (x = getHash(deq.poll()))) { hashes.remove(x); } deq.add(i); } else { hashes.add(h); deq.add(i); } if (max <= hashes.size()) { max = hashes.size(); ans = deq.peek(); } } System.out.println(ans); } static int getHash(int x) { if (x < 10) { return x; } else { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return getHash(sum); } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }