結果

問題 No.12 限定された素数
ユーザー fujisufujisu
提出日時 2015-02-04 04:24:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 238 ms / 5,000 ms
コード長 3,594 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 76,856 KB
実行使用メモリ 81,628 KB
最終ジャッジ日時 2023-08-15 22:32:01
合計ジャッジ時間 9,697 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 230 ms
81,188 KB
testcase_01 AC 231 ms
81,192 KB
testcase_02 AC 228 ms
81,536 KB
testcase_03 AC 222 ms
81,280 KB
testcase_04 AC 228 ms
81,304 KB
testcase_05 AC 231 ms
81,344 KB
testcase_06 AC 232 ms
81,272 KB
testcase_07 AC 234 ms
81,136 KB
testcase_08 AC 230 ms
81,200 KB
testcase_09 AC 231 ms
81,336 KB
testcase_10 AC 231 ms
81,156 KB
testcase_11 AC 237 ms
81,628 KB
testcase_12 AC 238 ms
81,432 KB
testcase_13 AC 229 ms
81,244 KB
testcase_14 AC 231 ms
81,180 KB
testcase_15 AC 226 ms
81,184 KB
testcase_16 AC 236 ms
81,236 KB
testcase_17 AC 228 ms
81,428 KB
testcase_18 AC 234 ms
81,372 KB
testcase_19 AC 232 ms
81,276 KB
testcase_20 AC 231 ms
81,356 KB
testcase_21 AC 232 ms
81,088 KB
testcase_22 AC 231 ms
81,132 KB
testcase_23 AC 231 ms
81,248 KB
testcase_24 AC 229 ms
81,204 KB
testcase_25 AC 231 ms
81,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
		}
	}
}
0