結果
問題 |
No.6 使いものにならないハッシュ
|
ユーザー |
![]() |
提出日時 | 2020-01-15 10:45:00 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 266 ms / 5,000 ms |
コード長 | 2,357 bytes |
コンパイル時間 | 2,318 ms |
コンパイル使用メモリ | 77,744 KB |
実行使用メモリ | 45,612 KB |
最終ジャッジ日時 | 2024-09-16 16:54:46 |
合計ジャッジ時間 | 9,947 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); int n = sc.nextInt(); Prime[] used = new Prime[10]; TreeSet<Prime> primes = new TreeSet<Prime>(); int max = 0; int maxNumber = 0; for (int i = k; i <= n; i++) { if (!isPrime(i)) { continue; } Prime p = new Prime(i); if (used[p.hashCode()] == null) { used[p.hashCode()] = p; primes.add(p); } else { Prime x = used[p.hashCode()]; used[p.hashCode()] = null; while (primes.lower(x) != null) { Prime tmp = primes.lower(x); primes.remove(x); x = tmp; used[x.hashCode()] = null; } primes.remove(x); used[p.hashCode()] = p; primes.add(p); } if (max <= primes.size()) { max = primes.size(); maxNumber = primes.first().number; } } System.out.println(maxNumber); } static class Prime implements Comparable<Prime> { int number; int hash; public Prime(int number) { this.number = number; this.hash = getHash(number); } public int hashCode() { return hash; } public boolean equals(Object o) { Prime another = (Prime) o; return number == another.number; } public int compareTo(Prime another) { return number - another.number; } private int getHash(int x) { while (x >= 10) { int y = x; x = 0; while (y > 0) { x += y % 10; y /= 10; } } return x; } } static boolean isPrime(int x) { if (x < 2) { return false; } for (int i = 2; i <= Math.sqrt(x); i++) { if (x % i == 0) { return false; } } return true; } }