結果

問題 No.12 限定された素数
ユーザー 夜
提出日時 2020-12-16 00:32:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 5,000 ms
コード長 1,474 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 97,704 KB
実行使用メモリ 15,804 KB
最終ジャッジ日時 2023-10-20 07:17:43
合計ジャッジ時間 9,034 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 257 ms
15,804 KB
testcase_01 AC 253 ms
15,804 KB
testcase_02 AC 251 ms
15,804 KB
testcase_03 AC 259 ms
15,804 KB
testcase_04 AC 249 ms
15,804 KB
testcase_05 AC 257 ms
15,804 KB
testcase_06 AC 256 ms
15,804 KB
testcase_07 AC 256 ms
15,804 KB
testcase_08 AC 251 ms
15,804 KB
testcase_09 AC 251 ms
15,804 KB
testcase_10 AC 254 ms
15,804 KB
testcase_11 AC 257 ms
15,804 KB
testcase_12 AC 253 ms
15,804 KB
testcase_13 AC 256 ms
15,804 KB
testcase_14 AC 255 ms
15,804 KB
testcase_15 AC 254 ms
15,804 KB
testcase_16 AC 256 ms
15,804 KB
testcase_17 AC 259 ms
15,804 KB
testcase_18 AC 256 ms
15,804 KB
testcase_19 AC 254 ms
15,804 KB
testcase_20 AC 255 ms
15,804 KB
testcase_21 AC 257 ms
15,804 KB
testcase_22 AC 252 ms
15,804 KB
testcase_23 AC 251 ms
15,804 KB
testcase_24 AC 255 ms
15,804 KB
testcase_25 AC 258 ms
15,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
bool d[10] = { false };
bool bo[5000050] = { false };
vector<vector<int>> vec(10);
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		d[a] = true;
	}
	bo[1] = true;
	for (int i = 2; i <= 5000000; i++) {
		if (!bo[i]) {
			for (int j = i + i; j <= 5000000; j += i) {
				bo[j] = true;
			}
			int i1 = i;
			bool d1[10] = { false };
			while (i1 > 0) {
				if (!d1[i1 % 10]) {
					vec[i1 % 10].emplace_back(i);
					d1[i1 % 10] = true;
				}
				i1 /= 10;
			}
		}
	}
	long long ans = -1;
	for (int i = 1; i <= 5000000; i++) {
		if (!bo[i - 1]) {
			long long m = 5000001;
			bool b = true;
			for (int j = 0; j < 10; j++) {
				if (!d[j]) {
					auto itr = lower_bound(vec[j].begin(), vec[j].end(), i);
					if (itr != vec[j].end()) {
						if (m > * itr) {
							m = *itr;
						}
					}
				}
			}
			for (int j = 0; j < 10; j++) {
				if (d[j]) {
					auto itr = lower_bound(vec[j].begin(), vec[j].end(), i);
					if (itr == vec[j].end()) {
						b = false;
					}
					else {
						if (*itr >= m) {
							b = false;
						}
					}
				}
			}
			if (b) {
				if (ans < m - i - 1) {
					ans = m - i - 1;
				}
			}
		}
	}
	cout << ans << endl;
}
0