結果

問題 No.12 限定された素数
ユーザー t8m8⛄️t8m8⛄️
提出日時 2016-02-12 19:12:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 309 ms / 5,000 ms
コード長 1,611 bytes
コンパイル時間 4,078 ms
コンパイル使用メモリ 79,144 KB
実行使用メモリ 66,016 KB
最終ジャッジ日時 2023-08-15 23:19:33
合計ジャッジ時間 13,209 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 296 ms
65,172 KB
testcase_01 AC 295 ms
65,772 KB
testcase_02 AC 294 ms
65,316 KB
testcase_03 AC 144 ms
55,976 KB
testcase_04 AC 294 ms
65,100 KB
testcase_05 AC 301 ms
65,696 KB
testcase_06 AC 302 ms
65,564 KB
testcase_07 AC 296 ms
65,632 KB
testcase_08 AC 296 ms
65,988 KB
testcase_09 AC 298 ms
65,252 KB
testcase_10 AC 297 ms
65,328 KB
testcase_11 AC 306 ms
65,924 KB
testcase_12 AC 309 ms
65,040 KB
testcase_13 AC 298 ms
65,488 KB
testcase_14 AC 300 ms
65,316 KB
testcase_15 AC 300 ms
65,652 KB
testcase_16 AC 303 ms
65,216 KB
testcase_17 AC 291 ms
65,296 KB
testcase_18 AC 297 ms
65,476 KB
testcase_19 AC 296 ms
65,460 KB
testcase_20 AC 299 ms
65,576 KB
testcase_21 AC 298 ms
66,016 KB
testcase_22 AC 295 ms
65,340 KB
testcase_23 AC 301 ms
65,324 KB
testcase_24 AC 300 ms
65,040 KB
testcase_25 AC 294 ms
65,676 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, (i+j+1 < m ? primes.get(i+j+1)-1 : 499999) - (i > 0 ? primes.get(i-1)+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