結果

問題 No.12 限定された素数
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-04 13:50:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,229 bytes
コンパイル時間 2,237 ms
コンパイル使用メモリ 76,880 KB
実行使用メモリ 85,120 KB
最終ジャッジ日時 2023-09-25 01:03:09
合計ジャッジ時間 15,592 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 455 ms
83,036 KB
testcase_02 AC 372 ms
77,848 KB
testcase_03 AC 411 ms
78,672 KB
testcase_04 WA -
testcase_05 AC 465 ms
83,072 KB
testcase_06 AC 462 ms
83,384 KB
testcase_07 AC 469 ms
83,032 KB
testcase_08 AC 452 ms
85,120 KB
testcase_09 AC 446 ms
83,280 KB
testcase_10 AC 447 ms
83,012 KB
testcase_11 AC 475 ms
83,176 KB
testcase_12 AC 468 ms
83,232 KB
testcase_13 AC 460 ms
83,212 KB
testcase_14 AC 461 ms
83,420 KB
testcase_15 AC 459 ms
83,088 KB
testcase_16 AC 497 ms
83,636 KB
testcase_17 AC 359 ms
77,808 KB
testcase_18 AC 363 ms
77,796 KB
testcase_19 AC 362 ms
77,672 KB
testcase_20 WA -
testcase_21 AC 437 ms
83,180 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 448 ms
82,600 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 = 0;

            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