結果

問題 No.6 使いものにならないハッシュ
ユーザー tentententen
提出日時 2022-08-18 18:26:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 120 ms / 5,000 ms
コード長 2,161 bytes
コンパイル時間 2,442 ms
コンパイル使用メモリ 78,648 KB
実行使用メモリ 40,536 KB
最終ジャッジ日時 2024-04-16 02:21:52
合計ジャッジ時間 6,291 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
36,904 KB
testcase_01 AC 51 ms
37,232 KB
testcase_02 AC 110 ms
40,488 KB
testcase_03 AC 59 ms
37,296 KB
testcase_04 AC 80 ms
38,936 KB
testcase_05 AC 85 ms
38,896 KB
testcase_06 AC 104 ms
39,492 KB
testcase_07 AC 89 ms
39,076 KB
testcase_08 AC 98 ms
39,232 KB
testcase_09 AC 91 ms
38,860 KB
testcase_10 AC 52 ms
36,872 KB
testcase_11 AC 65 ms
37,636 KB
testcase_12 AC 112 ms
40,000 KB
testcase_13 AC 87 ms
38,712 KB
testcase_14 AC 79 ms
39,072 KB
testcase_15 AC 107 ms
39,340 KB
testcase_16 AC 94 ms
39,104 KB
testcase_17 AC 106 ms
40,536 KB
testcase_18 AC 120 ms
40,404 KB
testcase_19 AC 106 ms
40,440 KB
testcase_20 AC 108 ms
39,416 KB
testcase_21 AC 55 ms
37,112 KB
testcase_22 AC 107 ms
39,996 KB
testcase_23 AC 104 ms
39,988 KB
testcase_24 AC 100 ms
39,656 KB
testcase_25 AC 95 ms
39,472 KB
testcase_26 AC 110 ms
40,336 KB
testcase_27 AC 93 ms
39,104 KB
testcase_28 AC 91 ms
39,032 KB
testcase_29 AC 113 ms
39,868 KB
testcase_30 AC 106 ms
40,256 KB
testcase_31 AC 109 ms
40,124 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