結果
問題 | No.12 限定された素数 |
ユーザー | fujisu |
提出日時 | 2015-02-04 04:24:52 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 247 ms / 5,000 ms |
コード長 | 3,594 bytes |
コンパイル時間 | 2,444 ms |
コンパイル使用メモリ | 79,940 KB |
実行使用メモリ | 68,156 KB |
最終ジャッジ日時 | 2024-11-24 07:39:36 |
合計ジャッジ時間 | 9,696 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 243 ms
67,616 KB |
testcase_01 | AC | 244 ms
67,616 KB |
testcase_02 | AC | 236 ms
67,480 KB |
testcase_03 | AC | 225 ms
67,816 KB |
testcase_04 | AC | 237 ms
67,756 KB |
testcase_05 | AC | 236 ms
67,904 KB |
testcase_06 | AC | 240 ms
67,480 KB |
testcase_07 | AC | 245 ms
67,936 KB |
testcase_08 | AC | 240 ms
67,932 KB |
testcase_09 | AC | 244 ms
67,892 KB |
testcase_10 | AC | 236 ms
67,892 KB |
testcase_11 | AC | 247 ms
68,156 KB |
testcase_12 | AC | 247 ms
67,868 KB |
testcase_13 | AC | 245 ms
67,816 KB |
testcase_14 | AC | 231 ms
67,804 KB |
testcase_15 | AC | 234 ms
67,744 KB |
testcase_16 | AC | 245 ms
67,608 KB |
testcase_17 | AC | 238 ms
67,816 KB |
testcase_18 | AC | 235 ms
67,704 KB |
testcase_19 | AC | 237 ms
67,480 KB |
testcase_20 | AC | 235 ms
67,856 KB |
testcase_21 | AC | 238 ms
67,952 KB |
testcase_22 | AC | 239 ms
67,728 KB |
testcase_23 | AC | 235 ms
67,856 KB |
testcase_24 | AC | 235 ms
67,640 KB |
testcase_25 | AC | 238 ms
67,932 KB |
ソースコード
import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; public class Main { int n; int[] a; int set(int k) { int ret = 0; while (0 < k) { ret |= (1 << k % 10); k /= 10; } return ret; } void run() { MyScanner sc = new MyScanner(); int T = 5000000 + 1; // int T = 100; boolean[] prime = new boolean[T]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 0; i * i <= T; i++) { if (prime[i]) { for (int j = i * i; j < T; j += i) { prime[j] = false; } } } List<Integer> tmp = new LinkedList<Integer>(); tmp.add(0); for (int i = 0; i < T; i++) { if (prime[i]) { tmp.add(i); } } tmp.add(T); Integer[] list = tmp.toArray(new Integer[0]); int[] bit = new int[list.length]; for (int i = 1; i < list.length - 1; i++) { bit[i] = set(list[i]); } int n = sc.nextInt(); int mask = 0; for (int i = 0; i < n; i++) { mask |= (1 << sc.nextInt()); } if (n < 10) { int max = -1; // System.out.println("mask: " + Integer.toBinaryString(mask)); for (int i = 1; i < list.length - 1; i++) { // System.out.println("i: " + list[i]); int set = 0; for (int j = i; j < list.length - 1; j++) { set |= bit[j]; // System.out.println(" set: " + // Integer.toBinaryString(set)); if (set == mask) { max = Math.max(max, (list[j + 1] - 1) - (list[i - 1] + 1)); } if ((set & mask) != set) { break; } } } System.out.println(max); } else { System.out.println(4999999); } } public static void main(String[] args) { new Main().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------" + '\n'); } class MyScanner { int read() { try { return System.in.read(); } catch (IOException e) { throw new InputMismatchException(); } } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }