結果

問題 No.12 限定された素数
ユーザー kenken714kenken714
提出日時 2019-04-18 22:06:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 1,804 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 88,360 KB
実行使用メモリ 14,796 KB
最終ジャッジ日時 2023-08-16 00:59:13
合計ジャッジ時間 3,532 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
12,944 KB
testcase_01 AC 49 ms
12,992 KB
testcase_02 AC 44 ms
12,464 KB
testcase_03 AC 100 ms
8,608 KB
testcase_04 AC 45 ms
13,104 KB
testcase_05 AC 55 ms
12,068 KB
testcase_06 AC 68 ms
12,396 KB
testcase_07 AC 78 ms
9,844 KB
testcase_08 AC 50 ms
11,984 KB
testcase_09 AC 49 ms
14,796 KB
testcase_10 AC 48 ms
12,728 KB
testcase_11 AC 96 ms
9,004 KB
testcase_12 AC 75 ms
9,800 KB
testcase_13 AC 61 ms
12,004 KB
testcase_14 AC 51 ms
12,656 KB
testcase_15 AC 62 ms
12,648 KB
testcase_16 AC 88 ms
9,488 KB
testcase_17 AC 45 ms
14,416 KB
testcase_18 AC 41 ms
12,880 KB
testcase_19 AC 41 ms
12,500 KB
testcase_20 AC 44 ms
12,112 KB
testcase_21 AC 48 ms
12,328 KB
testcase_22 AC 44 ms
13,344 KB
testcase_23 AC 43 ms
12,236 KB
testcase_24 AC 43 ms
12,176 KB
testcase_25 AC 56 ms
14,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>

using namespace std;

#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, ans = -1;
vector<ll> p, list, able;

bool IsPrime(ll n) {
	if (n <= 1)return false;
	for (ll i = 2; i <= sqrt(n); i++)
	{
		if (n % i == 0)return false;
	}
	return true;
}

vector<ll> era(ll n) {
	vector<bool> ok(n + 1);
	vector<ll> res;
	for (ll i = 2; i <= sqrt(n); ++i) {
		if (IsPrime(i)) {
			for (ll j = i * i; j <= n; j += i) ok[j] = true;
		}
	}
	FOR(i, 2, n + 1)if (!ok[i])res.push_back(i);
	return res;
}

bool check(ll a) {
	while (a > 0) {
		bool ok = false;
		REP(i, able.size()) if (a % 10 == able[i]) ok = true;
		if (!ok)return true;
		a /= 10;
	}
	return false;
}

int main() {
	p = era(5000000);
	cin >> n;
	REP(i, n) {
		ll a;
		cin >> a;
		able.push_back(a);
	}
	list.push_back(-1);
	REP(i, p.size()) {
		if (check(p[i]))list.push_back(i);
	}
	list.push_back(p.size());
	REP(i, list.size() - 1) {
		set<ll> se;
		FOR(j, list[i] + 1, list[i + 1]) {
			ll a = p[j];
			while (a > 0) {
				se.insert(a % 10);
				a /= 10;
			}
		}
		if (se.size() == able.size()) {
			ll l, r;
			if (list[i] == -1)l = 1;
			else l = p[list[i]] + 1;
			if (list[i + 1] == p.size())r = 5000000;
			else r = p[list[i + 1]] - 1;
			ans = max(ans, r - l);
		}
		
	}
	cout << ans << endl;
}



0