結果

問題 No.12 限定された素数
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-04 13:53:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 486 ms / 5,000 ms
コード長 3,229 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 76,560 KB
実行使用メモリ 83,720 KB
最終ジャッジ日時 2023-08-15 23:02:19
合計ジャッジ時間 14,827 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 368 ms
77,828 KB
testcase_01 AC 444 ms
83,364 KB
testcase_02 AC 365 ms
77,688 KB
testcase_03 AC 404 ms
78,788 KB
testcase_04 AC 429 ms
82,084 KB
testcase_05 AC 446 ms
83,240 KB
testcase_06 AC 457 ms
83,392 KB
testcase_07 AC 453 ms
83,380 KB
testcase_08 AC 439 ms
83,268 KB
testcase_09 AC 440 ms
83,020 KB
testcase_10 AC 451 ms
82,888 KB
testcase_11 AC 455 ms
83,096 KB
testcase_12 AC 457 ms
82,968 KB
testcase_13 AC 443 ms
83,032 KB
testcase_14 AC 449 ms
83,100 KB
testcase_15 AC 443 ms
82,832 KB
testcase_16 AC 486 ms
83,720 KB
testcase_17 AC 358 ms
78,024 KB
testcase_18 AC 363 ms
77,764 KB
testcase_19 AC 362 ms
77,776 KB
testcase_20 AC 423 ms
82,408 KB
testcase_21 AC 437 ms
83,316 KB
testcase_22 AC 364 ms
77,676 KB
testcase_23 AC 365 ms
77,996 KB
testcase_24 AC 365 ms
77,708 KB
testcase_25 AC 439 ms
83,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    static boolean[] a = new boolean[10];

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        for (int i = 0; i < n; i++) {
            a[sc.nextInt()] = true;
        }

        ArrayList<Integer> prime = getPrime();

        int miniMax = Integer.MIN_VALUE;

        for (int i = 0; i < prime.size(); i++) {
            TreeMap<Integer, Integer> nums = getNum(prime.get(i));

            if (!isTrue(nums)) {
                continue;
            }

            int miniIndex = i;
            int maxIndex = i;

            i++;

            boolean[] tmpA = a.clone();
            cieve(tmpA, nums);

            for (; i < prime.size(); i++) {
                nums = getNum(prime.get(i));

                if (!isTrue(nums)) {
                    break;
                }
                maxIndex = i;
                cieve(tmpA, nums);
            }

            boolean include = true;

            for (boolean a : tmpA) {
                if (a) {
                    include = false;
                    break;
                }
            }
            if (!include) {
                continue;
            }

            int min;
            int max;

            if (miniIndex == 0) {
                min = 1;
            } else {
                min = prime.get(miniIndex - 1) + 1;
            }

            if (maxIndex == prime.size() - 1) {
                max = 5000000;
            } else {
                max = prime.get(maxIndex + 1) - 1;
            }

            if (miniMax < max - min) {
                miniMax = max - min;
            }
        }

        if (miniMax == Integer.MIN_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(miniMax);
        }
    }

    private static ArrayList<Integer> getPrime() {
        boolean[] cieve = new boolean[5000001];

        cieve[0] = true;
        cieve[1] = true;

        int max = (int) Math.sqrt(5000000);

        for (int i = 2; i <= max; i++) {

            if (cieve[i]) {
                continue;
            }
            for (int j = i; j * i < 5000000; j++) {
                cieve[j * i] = true;
            }
        }

        ArrayList<Integer> ret = new ArrayList();

        for (int i = 0; i < 5000000; i++) {
            if (!cieve[i]) {
                ret.add(i);
            }
        }

        return ret;
    }

    private static TreeMap<Integer, Integer> getNum(int num) {
        TreeMap<Integer, Integer> ret = new TreeMap();

        while (num != 0) {
            ret.put(num % 10, 0);
            num = num / 10;
        }

        return ret;
    }

    private static boolean isTrue(TreeMap<Integer, Integer> nums) {
        for (int num : nums.keySet()) {
            if (!a[num]) {
                return false;
            }
        }
        return true;
    }

    private static void cieve(boolean[] tmpA, TreeMap<Integer, Integer> nums) {
        for (int num : nums.keySet()) {
            tmpA[num] = false;
        }
    }
}
0