結果

問題 No.6 使いものにならないハッシュ
ユーザー htensaihtensai
提出日時 2020-01-15 10:45:00
言語 Java19
(openjdk 21)
結果
AC  
実行時間 272 ms / 5,000 ms
コード長 2,357 bytes
コンパイル時間 2,558 ms
コンパイル使用メモリ 79,408 KB
実行使用メモリ 61,016 KB
最終ジャッジ日時 2023-10-14 23:22:09
合計ジャッジ時間 10,770 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
55,816 KB
testcase_01 AC 129 ms
55,736 KB
testcase_02 AC 272 ms
60,016 KB
testcase_03 AC 150 ms
56,080 KB
testcase_04 AC 158 ms
56,136 KB
testcase_05 AC 196 ms
56,356 KB
testcase_06 AC 226 ms
58,836 KB
testcase_07 AC 169 ms
55,740 KB
testcase_08 AC 194 ms
56,336 KB
testcase_09 AC 154 ms
55,896 KB
testcase_10 AC 130 ms
55,668 KB
testcase_11 AC 158 ms
55,944 KB
testcase_12 AC 234 ms
58,100 KB
testcase_13 AC 149 ms
55,864 KB
testcase_14 AC 153 ms
56,036 KB
testcase_15 AC 220 ms
56,372 KB
testcase_16 AC 174 ms
56,104 KB
testcase_17 AC 252 ms
59,660 KB
testcase_18 AC 270 ms
61,016 KB
testcase_19 AC 246 ms
59,452 KB
testcase_20 AC 237 ms
58,884 KB
testcase_21 AC 148 ms
55,820 KB
testcase_22 AC 243 ms
59,292 KB
testcase_23 AC 239 ms
56,416 KB
testcase_24 AC 243 ms
59,192 KB
testcase_25 AC 196 ms
56,312 KB
testcase_26 AC 257 ms
59,476 KB
testcase_27 AC 237 ms
59,344 KB
testcase_28 AC 197 ms
54,136 KB
testcase_29 AC 259 ms
59,372 KB
testcase_30 AC 252 ms
57,816 KB
testcase_31 AC 243 ms
59,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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