結果

問題 No.6 使いものにならないハッシュ
ユーザー tentententen
提出日時 2022-08-18 18:26:41
言語 Java21
(openjdk 21)
結果
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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
36,464 KB
testcase_01 AC 49 ms
36,544 KB
testcase_02 AC 111 ms
40,576 KB
testcase_03 AC 68 ms
37,900 KB
testcase_04 AC 80 ms
38,432 KB
testcase_05 AC 74 ms
38,508 KB
testcase_06 AC 101 ms
39,372 KB
testcase_07 AC 83 ms
38,376 KB
testcase_08 AC 88 ms
39,524 KB
testcase_09 AC 74 ms
38,816 KB
testcase_10 AC 46 ms
36,896 KB
testcase_11 AC 68 ms
36,832 KB
testcase_12 AC 98 ms
39,808 KB
testcase_13 AC 66 ms
38,596 KB
testcase_14 AC 72 ms
38,708 KB
testcase_15 AC 99 ms
38,940 KB
testcase_16 AC 87 ms
39,052 KB
testcase_17 AC 99 ms
39,972 KB
testcase_18 AC 113 ms
39,936 KB
testcase_19 AC 98 ms
39,396 KB
testcase_20 AC 102 ms
39,452 KB
testcase_21 AC 54 ms
36,668 KB
testcase_22 AC 103 ms
39,652 KB
testcase_23 AC 105 ms
39,040 KB
testcase_24 AC 100 ms
39,704 KB
testcase_25 AC 74 ms
38,584 KB
testcase_26 AC 104 ms
40,300 KB
testcase_27 AC 98 ms
39,272 KB
testcase_28 AC 85 ms
38,732 KB
testcase_29 AC 98 ms
40,140 KB
testcase_30 AC 98 ms
40,092 KB
testcase_31 AC 100 ms
39,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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