結果

問題 No.12 限定された素数
ユーザー t8m8⛄️t8m8⛄️
提出日時 2016-02-12 19:09:51
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,578 bytes
コンパイル時間 4,797 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 67,384 KB
最終ジャッジ日時 2023-10-22 02:37:31
合計ジャッジ時間 13,027 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 245 ms
66,620 KB
testcase_01 AC 257 ms
54,228 KB
testcase_02 AC 264 ms
54,260 KB
testcase_03 AC 128 ms
41,212 KB
testcase_04 AC 258 ms
54,360 KB
testcase_05 AC 262 ms
54,264 KB
testcase_06 AC 265 ms
54,224 KB
testcase_07 AC 258 ms
54,264 KB
testcase_08 AC 257 ms
54,260 KB
testcase_09 AC 256 ms
54,424 KB
testcase_10 AC 261 ms
54,328 KB
testcase_11 RE -
testcase_12 AC 270 ms
54,268 KB
testcase_13 AC 263 ms
54,264 KB
testcase_14 AC 253 ms
54,380 KB
testcase_15 AC 262 ms
54,396 KB
testcase_16 AC 261 ms
66,640 KB
testcase_17 AC 249 ms
54,312 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 247 ms
54,260 KB
testcase_21 AC 257 ms
54,264 KB
testcase_22 AC 263 ms
54,324 KB
testcase_23 AC 249 ms
54,288 KB
testcase_24 AC 262 ms
54,316 KB
testcase_25 AC 256 ms
54,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class No0012 {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static boolean debug = false;

	static void solve() {
		int n = in.nextInt();
		int bit = 0;
		for (int i=0; i<n; i++) {
			bit |= 1<<in.nextInt();
		}

		if (bit == 1023) {
			out.println("4999999");
			return;
		}

		BitSet isp = new BitSet();
		isp.flip(2, 5000001);
		ArrayList<Integer> primes = new ArrayList<>();
		for (int i=2; i*i<=5000001; i=isp.nextSetBit(i+1)) {
			for (int j=i+i; j<5000001; j+=i) {
				isp.set(j, false);
			}
		}

		for (int i=2; i>=0; i=isp.nextSetBit(i+1)) {
			primes.add(i);
		}

		dump(isp.cardinality());

		dump(primes);

		int m = primes.size();
		int[] mask = new int[m];
		int pos = 0;
		for (int p : primes) {
			int b = 0;
			while (p > 0) {
				b |= 1<<(p%10);
				p /= 10;
			}
			mask[pos++] = b;
		}


		int ans = -1;
		for (int i=0; i<m; i++) {
			int b = 0;
			for (int j=0; i+j<m; j++) {
				b |= mask[i+j];
				if ((b&(~bit)) > 0) break;
				if (b == bit) {
					ans = Math.max(ans, (primes.get(i+j+1)-1) - (primes.get(i-1)+1));
				}
			}
		}

		out.println(ans);
	}

	public static void main(String[] args) {
		debug = args.length > 0;
		long start = System.currentTimeMillis();

		solve();
		out.flush();

		long end = System.currentTimeMillis();
		dump((end-start) + "ms");
		in.close();
		out.close();
	}

	static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
}
0