結果

問題 No.6 使いものにならないハッシュ
ユーザー GrenacheGrenache
提出日時 2016-03-05 15:13:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 215 ms / 5,000 ms
コード長 1,893 bytes
コンパイル時間 3,835 ms
コンパイル使用メモリ 76,932 KB
実行使用メモリ 59,896 KB
最終ジャッジ日時 2023-10-14 22:58:22
合計ジャッジ時間 10,406 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,912 KB
testcase_01 AC 123 ms
55,668 KB
testcase_02 AC 203 ms
59,340 KB
testcase_03 AC 160 ms
56,008 KB
testcase_04 AC 173 ms
56,288 KB
testcase_05 AC 174 ms
59,896 KB
testcase_06 AC 198 ms
57,832 KB
testcase_07 AC 177 ms
59,444 KB
testcase_08 AC 181 ms
59,560 KB
testcase_09 AC 172 ms
56,368 KB
testcase_10 AC 124 ms
56,028 KB
testcase_11 AC 172 ms
55,772 KB
testcase_12 AC 194 ms
58,476 KB
testcase_13 AC 145 ms
56,016 KB
testcase_14 AC 168 ms
55,688 KB
testcase_15 AC 188 ms
58,224 KB
testcase_16 AC 176 ms
58,940 KB
testcase_17 AC 182 ms
59,052 KB
testcase_18 AC 207 ms
59,812 KB
testcase_19 AC 193 ms
58,616 KB
testcase_20 AC 197 ms
58,636 KB
testcase_21 AC 156 ms
55,904 KB
testcase_22 AC 188 ms
58,820 KB
testcase_23 AC 195 ms
57,276 KB
testcase_24 AC 207 ms
58,388 KB
testcase_25 AC 180 ms
59,200 KB
testcase_26 AC 201 ms
58,396 KB
testcase_27 AC 209 ms
57,952 KB
testcase_28 AC 175 ms
58,456 KB
testcase_29 AC 196 ms
58,828 KB
testcase_30 AC 197 ms
58,808 KB
testcase_31 AC 215 ms
58,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.Scanner;


public class Main_yukicoder6 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int k = sc.nextInt();
        int n = sc.nextInt();

        List<Integer> primes = new ArrayList<Integer>();

        BitSet sieve = new BitSet();

        if (k <= 2) {
        	primes.add(2);
        }

        for (int i = 1; 2 * i + 1 <= n; i++) {
        	if (!sieve.get(i)) {
        		int p = 2 * i + 1;
        		if (p >= k) {
        			primes.add(p);
        		}

        		for (int j = 1; (i + (2 * i + 1) * j) * 2 + 1 <= n; j++) {
        			sieve.set(i + (2 * i + 1) * j);
        		}
        	}
        }

        int[] prev = new int[10];
        Arrays.fill(prev, -1);
        List<Integer> dp = new ArrayList<Integer>();
        List<Integer> hash = new ArrayList<Integer>();

        int max = 0;
        int maxp = 0;
        for (int i = 0; i < primes.size(); i++) {
        	int p = primes.get(i);

        	String s = Integer.toString(p);
        	while (s.length() > 1) {
        		int sum = 0;
        		for (char c : s.toCharArray()) {
        			sum += c - '0';
        		}

        		s = Integer.toString(sum);
        	}

        	hash.add(Integer.parseInt(s));

        	int tmp = i - prev[hash.get(i)];
        	for (int j = dp.size() - 1; j >= 0 && j >= dp.size() - 10; j--) {
        		if (hash.get(j) == hash.get(i)) {
        			tmp = Math.min(tmp, i - j);
        		} else {
        			tmp = Math.min(tmp, dp.get(j) + i - j);
        		}
        	}

        	dp.add(tmp);
        	if (tmp >= max) {
        		max = Math.max(max, tmp);
        		maxp = primes.get(i - tmp + 1);
        	}
        }

//        System.out.println(max);
        System.out.println(maxp);

        sc.close();
    }
}
0