結果

問題 No.12 限定された素数
ユーザー tentententen
提出日時 2023-01-26 08:07:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 199 ms / 5,000 ms
コード長 2,926 bytes
コンパイル時間 2,788 ms
コンパイル使用メモリ 90,040 KB
実行使用メモリ 64,792 KB
最終ジャッジ日時 2023-09-09 12:34:13
合計ジャッジ時間 9,011 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 190 ms
64,088 KB
testcase_01 AC 191 ms
64,552 KB
testcase_02 AC 186 ms
64,072 KB
testcase_03 AC 178 ms
64,340 KB
testcase_04 AC 189 ms
64,264 KB
testcase_05 AC 190 ms
64,792 KB
testcase_06 AC 193 ms
64,472 KB
testcase_07 AC 193 ms
64,488 KB
testcase_08 AC 191 ms
64,416 KB
testcase_09 AC 191 ms
64,508 KB
testcase_10 AC 191 ms
64,200 KB
testcase_11 AC 191 ms
64,204 KB
testcase_12 AC 187 ms
64,208 KB
testcase_13 AC 187 ms
64,528 KB
testcase_14 AC 193 ms
64,220 KB
testcase_15 AC 193 ms
64,292 KB
testcase_16 AC 192 ms
64,708 KB
testcase_17 AC 188 ms
63,992 KB
testcase_18 AC 188 ms
64,292 KB
testcase_19 AC 199 ms
64,088 KB
testcase_20 AC 189 ms
64,256 KB
testcase_21 AC 196 ms
64,360 KB
testcase_22 AC 191 ms
64,712 KB
testcase_23 AC 197 ms
64,396 KB
testcase_24 AC 192 ms
64,080 KB
testcase_25 AC 196 ms
62,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int target = 0;
        for (int i = 0; i < n; i++) {
            target += (1 << sc.nextInt());
        }
        ArrayList<Integer> primes = new ArrayList<>();
        primes.add(0);
        boolean[] isNotPrimes = new boolean[5000001];
        for (int i = 2; i < isNotPrimes.length; i++) {
            if (!isNotPrimes[i]) {
                primes.add(i);
                for (int j = 2; j * i < isNotPrimes.length; j++) {
                    isNotPrimes[j * i] = true;
                }
            }
        }
        primes.add(5000001);
        int left = 1;
        int[] counts = new int[10];
        int ans = -1;
        for (int i = 1; i < primes.size() - 1; i++) {
            addCount(counts, primes.get(i));
            int current = getMask(counts);
            if (current == target) {
                ans = Math.max(ans, primes.get(i + 1) - primes.get(left - 1) - 2);
            } else if ((current ^ (current & target)) > 0) {
                left = i + 1;
                Arrays.fill(counts, 0);
            }
        }
        System.out.println(ans);
    }
    
    static int getMask(int[] counts) {
        int mask = 0;
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] > 0) {
                mask += (1 << i);
            }
        }
        return mask;
    }
    
    static void addCount(int[] counts, int value) {
        while (value > 0) {
            counts[value % 10]++;
            value /= 10;
        }
    }    
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0