結果

問題 No.6 使いものにならないハッシュ
ユーザー tentententen
提出日時 2021-11-10 08:46:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 2,335 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 78,584 KB
実行使用メモリ 53,440 KB
最終ジャッジ日時 2024-04-30 19:14:07
合計ジャッジ時間 6,231 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
50,464 KB
testcase_01 AC 51 ms
50,416 KB
testcase_02 AC 113 ms
53,440 KB
testcase_03 AC 63 ms
51,264 KB
testcase_04 AC 86 ms
51,880 KB
testcase_05 AC 91 ms
51,648 KB
testcase_06 AC 95 ms
53,040 KB
testcase_07 AC 89 ms
51,896 KB
testcase_08 AC 99 ms
52,092 KB
testcase_09 AC 88 ms
51,948 KB
testcase_10 AC 54 ms
50,304 KB
testcase_11 AC 78 ms
51,240 KB
testcase_12 AC 106 ms
53,004 KB
testcase_13 AC 82 ms
51,900 KB
testcase_14 AC 81 ms
51,916 KB
testcase_15 AC 101 ms
52,224 KB
testcase_16 AC 88 ms
51,764 KB
testcase_17 AC 106 ms
52,244 KB
testcase_18 AC 109 ms
52,956 KB
testcase_19 AC 102 ms
53,108 KB
testcase_20 AC 108 ms
52,136 KB
testcase_21 AC 57 ms
50,552 KB
testcase_22 AC 107 ms
51,980 KB
testcase_23 AC 101 ms
52,648 KB
testcase_24 AC 100 ms
52,724 KB
testcase_25 AC 97 ms
52,184 KB
testcase_26 AC 103 ms
52,876 KB
testcase_27 AC 98 ms
52,864 KB
testcase_28 AC 87 ms
52,184 KB
testcase_29 AC 104 ms
51,896 KB
testcase_30 AC 106 ms
52,828 KB
testcase_31 AC 103 ms
52,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
}
0