結果

問題 No.12 限定された素数
ユーザー furonfuron
提出日時 2023-05-30 15:32:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 2,159 bytes
コンパイル時間 1,323 ms
コンパイル使用メモリ 130,324 KB
実行使用メモリ 5,972 KB
最終ジャッジ日時 2023-08-28 00:39:37
合計ジャッジ時間 4,059 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
5,764 KB
testcase_01 AC 45 ms
5,688 KB
testcase_02 AC 44 ms
5,688 KB
testcase_03 AC 40 ms
5,644 KB
testcase_04 AC 43 ms
5,736 KB
testcase_05 AC 45 ms
5,688 KB
testcase_06 AC 43 ms
5,640 KB
testcase_07 AC 42 ms
5,680 KB
testcase_08 AC 44 ms
5,860 KB
testcase_09 AC 43 ms
5,736 KB
testcase_10 AC 45 ms
5,788 KB
testcase_11 AC 41 ms
5,740 KB
testcase_12 AC 44 ms
5,712 KB
testcase_13 AC 44 ms
5,640 KB
testcase_14 AC 45 ms
5,736 KB
testcase_15 AC 44 ms
5,844 KB
testcase_16 AC 45 ms
5,732 KB
testcase_17 AC 43 ms
5,732 KB
testcase_18 AC 43 ms
5,852 KB
testcase_19 AC 43 ms
5,640 KB
testcase_20 AC 44 ms
5,692 KB
testcase_21 AC 44 ms
5,700 KB
testcase_22 AC 43 ms
5,736 KB
testcase_23 AC 43 ms
5,972 KB
testcase_24 AC 43 ms
5,792 KB
testcase_25 AC 44 ms
5,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
//const int MOD = 1000000007;
const int MOD = 998244353;

vi getPrimes(int N) {
	vector<bool> isPrime(N + 1, true);
	isPrime[0] = isPrime[1] = false;

	for (int p = 2; p * p <= N; ++p) {
		if (!isPrime[p]) continue;

		for (int q = p * 2; q <= N; q += p) {
			isPrime[q] = false;
		}
	}
	vi ret;
	ret.push_back(1);
	for (int i = 2; i < N; i++) {
		if (isPrime[i]) ret.push_back(i);
	}
	ret.push_back(N + 1);
	return ret;
}

int check(vi& cnt, vb& tgt) {
	int cnt1 = 0;
	int cnt2 = 0;
	for (int i = 0; i < 10; i++) {
		if (!tgt[i] && cnt[i] > 0) return 2;
		if (tgt[i]) {
			cnt2++;
			if (cnt[i] > 0) cnt1++;
		}
	}
	if (cnt1 < cnt2) return 1;

	return 0;
}

void count(int x, vi& cnt) {
	while (x > 0) {
		cnt[x % 10]++;
		x /= 10;
	}
}

int main() {
	int n;
	cin >> n;
	vi a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vb tgt(10, false);
	for (int i = 0; i < n; i++) {
		tgt[a[i]] = true;
	}
	vi cnt(10, 0);
	vi p = getPrimes(5000000);
	int L = 1, K = 1, ans = -1;

	int r = 1;
	int ma = p.size() - 1;
	bool ok = false;

	while (r <= ma) {
		int ch = check(cnt, tgt);
		while (r <= ma && ch == 1) {
			count(p[r], cnt);
			ch = check(cnt, tgt);
			r++;
		}
		if (ch == 0) {
			while (r <= ma && ch == 0) {
				count(p[r], cnt);
				ch = check(cnt, tgt);
				r++;
			}
			K = p[r - 1] - 1;

			ans = max(ans, K - L);
		}

		L = p[r - 1] + 1;
		cnt.assign(10, 0);

	}
	cout << ans << endl;

	return 0;
}
0