結果

問題 No.12 限定された素数
ユーザー pekempeypekempey
提出日時 2015-08-18 17:12:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 69 ms / 5,000 ms
コード長 1,544 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 148,436 KB
実行使用メモリ 11,180 KB
最終ジャッジ日時 2023-08-15 23:05:22
合計ジャッジ時間 4,625 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
11,180 KB
testcase_01 AC 57 ms
11,096 KB
testcase_02 AC 56 ms
10,988 KB
testcase_03 AC 69 ms
10,860 KB
testcase_04 AC 56 ms
11,008 KB
testcase_05 AC 57 ms
10,876 KB
testcase_06 AC 59 ms
11,064 KB
testcase_07 AC 61 ms
11,080 KB
testcase_08 AC 57 ms
10,984 KB
testcase_09 AC 56 ms
11,100 KB
testcase_10 AC 57 ms
10,852 KB
testcase_11 AC 65 ms
10,848 KB
testcase_12 AC 61 ms
11,004 KB
testcase_13 AC 58 ms
11,084 KB
testcase_14 AC 56 ms
10,928 KB
testcase_15 AC 58 ms
11,096 KB
testcase_16 AC 63 ms
11,072 KB
testcase_17 AC 56 ms
11,164 KB
testcase_18 AC 58 ms
11,028 KB
testcase_19 AC 57 ms
11,000 KB
testcase_20 AC 57 ms
11,004 KB
testcase_21 AC 57 ms
11,136 KB
testcase_22 AC 58 ms
11,100 KB
testcase_23 AC 56 ms
10,972 KB
testcase_24 AC 56 ms
11,096 KB
testcase_25 AC 57 ms
11,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

const int maxn = 5000001;
bool isprime[maxn];
vector<int> prime;
vector<int> digit;

int decomp(int n) {
	int res = 0;
	while (n) {
		res |= 1 << n % 10;
		n /= 10;
	}
	return res;
}

void sieve() {
	fill(isprime, isprime + maxn, true);
	for (ll i = 2; i < maxn; i++) {
		if (isprime[i]) {
			prime.push_back(i);
			digit.push_back(decomp(i));
			for (ll j = i * i; j < maxn; j += i) {
				isprime[j] = false;
			}
		}
	}
}

bool issubset(int a, int b) {
	return (a | b) == b;	
}

int main() {
	sieve();
	int N;
	cin >> N;
	int A = 0;
	rep (i, N) {
		int temp;
		cin >> temp;
		A |= 1 << temp;
	}

	int r = 0;
	int curr = 0;
	vector<int> num(10);
	int ans = -1;
	rep (i, prime.size()) {
		r = max(r, i);
		while (r < prime.size() && issubset(curr | digit[r], A)) {
			curr |= digit[r];
			rep (j, 10) {
				if (digit[r] >> j & 1) num[j]++;
			}
			r++;
		}

		if (curr == A) {
			int ub, lb;
			if (r == prime.size()) {
				ub = 5000000;
			} else {
				ub = prime[r] - 1;
			}
			if (i == 0) {
				lb = 1;
			} else {
				lb = prime[i - 1] + 1;
			}
			ans = max(ans, ub - lb);
		}

		curr = 0;
		rep (j, 10) {
			if (digit[i] >> j & 1) num[j]--;
			if (num[j] > 0) curr |= 1 << j;	
		}
	}

	cout << ans << endl;
}
0