結果

問題 No.12 限定された素数
ユーザー lovablepleiadlovablepleiad
提出日時 2015-12-05 13:57:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 138 ms / 5,000 ms
コード長 2,672 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 96,172 KB
実行使用メモリ 13,460 KB
最終ジャッジ日時 2023-08-15 23:11:11
合計ジャッジ時間 5,825 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
12,260 KB
testcase_01 AC 129 ms
12,704 KB
testcase_02 AC 124 ms
12,244 KB
testcase_03 AC 127 ms
13,028 KB
testcase_04 AC 123 ms
13,460 KB
testcase_05 AC 135 ms
12,664 KB
testcase_06 AC 130 ms
12,328 KB
testcase_07 AC 134 ms
12,256 KB
testcase_08 AC 137 ms
12,392 KB
testcase_09 AC 134 ms
12,456 KB
testcase_10 AC 138 ms
12,476 KB
testcase_11 AC 137 ms
12,248 KB
testcase_12 AC 135 ms
13,380 KB
testcase_13 AC 137 ms
13,456 KB
testcase_14 AC 134 ms
12,664 KB
testcase_15 AC 137 ms
12,396 KB
testcase_16 AC 135 ms
13,000 KB
testcase_17 AC 124 ms
12,916 KB
testcase_18 AC 127 ms
13,440 KB
testcase_19 AC 125 ms
12,924 KB
testcase_20 AC 126 ms
12,128 KB
testcase_21 AC 131 ms
12,652 KB
testcase_22 AC 127 ms
12,344 KB
testcase_23 AC 126 ms
12,660 KB
testcase_24 AC 125 ms
12,852 KB
testcase_25 AC 135 ms
12,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <stack>
#include <array>
#include <fstream>
#include <deque>
#include <bitset>


#define REP(i,n) for(int (i) = 0;(i) < (n) ; ++(i))
#define REPS(a,i,n) for(int (i) = (a) ; (i) < (n) ; ++(i))
#if defined(_MSC_VER)||__cplusplus > 199711L
#define AUTO(r,v) auto r = (v)
#else
#define AUTO(r,v) typeof(v) r = (v)
#endif
#define ALL(c) (c).begin() , (c).end()
#define EACH(it,c) for(AUTO(it,(c).begin());it != (c).end();++it)
#define LL long long
#define int LL
#define inf  ((int)1 << 54)
#define DIV 1000000007
#define QUICK_CIN ios::sync_with_stdio(false); cin.tie(0);
#define InitArr1(c,n) memset(&c[0],0,sizeof(int)*n)
#define InitArr2(c,n) memset(&c[0][0],0,sizeof(int)*n)

template<class T>
bool valid(T x, T w) { return 0 <= x&&x < w; }

int di[4] = { 1, -1, 0, 0 };
int dj[4] = { 0, 0, 1, -1 };

using namespace std;

//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------

bool prime[5000001];
vector<int> pris;


signed main()
{
	QUICK_CIN;
	//fstream cin("debug.txt");
	//ofstream cout("result.txt");

	prime[0] = prime[1] = true;

	for (int i = 2; i*i < 5000001; ++i) {
		int j = 2 * i;
		for (; j < 5000001; j += i) {
			prime[j] = true;
		}
	}

	pris.push_back(0);

	REP(i, 5000001) {
		if (!prime[i]) {
			pris.push_back(i);
		}
	}

	int n;
	cin >> n;
	map<int, bool> a;
	bool dd[10] = { false };

	REP(i, n) {
		int c;
		cin >> c;
		a[c] = true;
		dd[c] = true;
	}


	int max_l = -1;
	int point = 0;

	REP(i, pris.size()) {
		bool ok = true;

		auto p = pris[i];
		bool d[10] = { false };

		while (p) {
			d[p % 10] = true;
			p /= 10;
		}

		REP(i, 10) {
			if (d[i] && !a.count(i)) {
				ok = false;
			}
		}

		if (ok) {
			REP(i, 10) {
				if(d[i])
					dd[i] = false;
			}

			if (i == pris.size() - 1) {
				auto lp = pris[point];
				max_l = max(max_l, 5000000 - lp - 1);
			}
		}
		else {
			bool ook = false;
			REP(i, 10) {
				ook |= dd[i];
			}

			if (!ook) {
				auto rp = pris[i];
				auto lp = pris[point];
				max_l = max(max_l, rp - lp - 2);
			}
			
			for (auto x : a) {
				dd[x.first] = true;
			}

			point = i;
		}
	}
	cout << max_l << endl;
}
0