結果
問題 |
No.6 使いものにならないハッシュ
|
ユーザー |
![]() |
提出日時 | 2021-11-10 08:46:21 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 117 ms / 5,000 ms |
コード長 | 2,335 bytes |
コンパイル時間 | 2,723 ms |
コンパイル使用メモリ | 78,652 KB |
実行使用メモリ | 40,596 KB |
最終ジャッジ日時 | 2024-11-20 16:46:17 |
合計ジャッジ時間 | 6,971 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int k = sc.nextInt(); int n = sc.nextInt(); boolean[] isNotPrime = new boolean[n + 1]; HashSet<Integer> used = new HashSet<>(); ArrayDeque<Integer> primes = new ArrayDeque<>(); int max = 0; int maxIdx = 0; int left = k; for (int i = 2; i <= n; i++) { if (isNotPrime[i]) { continue; } for (int j = 2; j * i <= n; j++) { isNotPrime[j * i] = true; } if (i < k) { continue; } int hash = getHash(i); if (used.contains(hash)) { while (true) { int remain = getHash(primes.poll()); if (hash == remain) { break; } used.remove(remain); } } primes.add(i); used.add(hash); if (max <= primes.size()) { max = primes.size(); maxIdx = primes.peek(); } } System.out.println(maxIdx); } 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 nextLine() throws Exception { return br.readLine(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }