結果

問題 No.12 限定された素数
ユーザー finefine
提出日時 2017-01-09 18:14:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 1,333 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 147,280 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 23:36:39
合計ジャッジ時間 6,816 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 159 ms
4,376 KB
testcase_01 AC 165 ms
4,384 KB
testcase_02 AC 160 ms
4,376 KB
testcase_03 AC 157 ms
4,380 KB
testcase_04 AC 156 ms
4,380 KB
testcase_05 AC 172 ms
4,376 KB
testcase_06 AC 154 ms
4,380 KB
testcase_07 AC 160 ms
4,380 KB
testcase_08 AC 162 ms
4,376 KB
testcase_09 AC 159 ms
4,380 KB
testcase_10 AC 158 ms
4,376 KB
testcase_11 AC 151 ms
4,376 KB
testcase_12 AC 171 ms
4,380 KB
testcase_13 AC 171 ms
4,376 KB
testcase_14 AC 177 ms
4,384 KB
testcase_15 AC 166 ms
4,376 KB
testcase_16 AC 181 ms
4,376 KB
testcase_17 AC 173 ms
4,380 KB
testcase_18 AC 148 ms
4,376 KB
testcase_19 AC 150 ms
4,376 KB
testcase_20 AC 155 ms
4,376 KB
testcase_21 AC 160 ms
4,376 KB
testcase_22 AC 152 ms
4,376 KB
testcase_23 AC 150 ms
4,380 KB
testcase_24 AC 165 ms
4,376 KB
testcase_25 AC 159 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

vector<bool> sieve(int n) {
	vector<bool> is_prime(n + 1, true);
	is_prime[0] = false;
	is_prime[1] = false;
	for (int i = 2; i <= n; i++) {
		if (!is_prime[i]) continue;
		for (int j = 2 * i; j <= n; j += i) is_prime[j] = false;
	}
	return is_prime;
}

void change(int x, int* used, int c) {
	used[x % 10] += c;
	int tmp = 10;
	while (x >= tmp) {
		used[x / tmp % 10] += c;
		tmp *= 10;
	}
}

bool check1(int* used, bool* memo) {
	bool res = false;
	for (int i = 0; i < 10; i++) {
		if (memo[i] || used[i] == 0) continue;
		res = true;
		break;
	}
	return res;
}

bool check2(int* used, bool* memo) {
	bool res = true;
	for (int i = 0; i < 10; i++) {
		if (memo[i] == (used[i] != 0)) continue;
		res = false;
		break; 
	}
	return res;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	bool memo[10] = {};
	for (int i = 0; i < N; i++) {
		int A;
		cin >> A;
		memo[A] = true;
	}

	vector<bool> p(sieve(5000000));
	int used[10] = {};
	int ans = -1;
	for (int K = 1, L = 1; L <= 5000000; L++) {
		if (p[L]) change(L, used, 1);
		if (check1(used, memo)) {
			while (K < L) {
				if (p[K]) change(K, used, -1);
				K++;
				if (!check1(used, memo)) break;
			}
		}
		if (check2(used, memo)) {
			ans = max(ans, L - K);
		}
	}
	cout << ans << endl;
	return 0;
}
0