結果
問題 | No.12 限定された素数 |
ユーザー |
![]() |
提出日時 | 2023-01-26 08:07:47 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 211 ms / 5,000 ms |
コード長 | 2,926 bytes |
コンパイル時間 | 2,686 ms |
コンパイル使用メモリ | 97,484 KB |
実行使用メモリ | 53,780 KB |
最終ジャッジ日時 | 2024-06-27 05:38:32 |
合計ジャッジ時間 | 9,095 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
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();}}