結果

問題 No.12 限定された素数
ユーザー newlife171128newlife171128
提出日時 2018-02-13 22:40:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 2,278 bytes
コンパイル時間 1,244 ms
コンパイル使用メモリ 147,348 KB
実行使用メモリ 10,300 KB
最終ジャッジ日時 2023-08-16 00:47:12
合計ジャッジ時間 3,863 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
10,136 KB
testcase_01 AC 59 ms
10,140 KB
testcase_02 AC 57 ms
9,932 KB
testcase_03 AC 50 ms
10,092 KB
testcase_04 AC 58 ms
9,944 KB
testcase_05 AC 59 ms
9,944 KB
testcase_06 AC 58 ms
10,132 KB
testcase_07 AC 58 ms
9,952 KB
testcase_08 AC 59 ms
10,112 KB
testcase_09 AC 59 ms
10,140 KB
testcase_10 AC 56 ms
10,004 KB
testcase_11 AC 57 ms
10,116 KB
testcase_12 AC 57 ms
10,080 KB
testcase_13 AC 57 ms
9,992 KB
testcase_14 AC 59 ms
10,016 KB
testcase_15 AC 57 ms
10,116 KB
testcase_16 AC 57 ms
9,940 KB
testcase_17 AC 55 ms
10,068 KB
testcase_18 AC 54 ms
10,140 KB
testcase_19 AC 53 ms
10,060 KB
testcase_20 AC 56 ms
10,168 KB
testcase_21 AC 57 ms
9,996 KB
testcase_22 AC 55 ms
10,300 KB
testcase_23 AC 55 ms
10,068 KB
testcase_24 AC 55 ms
10,140 KB
testcase_25 AC 58 ms
10,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "bits/stdc++.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype>
#include <stdio.h>
#include <map>
#include <unordered_map>
#include <string.h>
#include <utility>

typedef long long ll;
const int INF = 1e8;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
using namespace std;

typedef pair<int, int> P;

const int saidai = 5000000;

bool judge[10];
bool prime[saidai + 1];
bool ok[10];

vector<int> prime_number;

void inite_ok() {
	for (int i = 0; i < 10; i++) {
		ok[i] = false;
	}
}

void sieve() {
	prime_number.push_back(1);
	for (int i = 2; i <= saidai; i++) {
		if (!prime[i]) {
			prime_number.push_back(i);
			for (int j = i * 2; j <= saidai; j += i) {
				prime[j] = true;
			}
		}
	}
}

bool bunkai(int x) {
	int s = 0;

	while (x != 0) {
		s = x % 10;
		x /= 10;
		if (!judge[s]) {
			return false;
		}
	}
	return true;
}
void ok_judge(int x) {
	int s = 0;
	while (x != 0) {
		s = x % 10;
		x /= 10;

		ok[s] = true;
	}
}
int main() {

	int n = 0;
	cin >> n;

	sieve();

	int tmp;
	for (int i = 0; i < n; i++) {
		cin >> tmp;
		judge[tmp] = true;
	}

	bool all = true;
	for (int i = 0; i < 10; i++) {
		if (judge[i] == true) {

		} else {
			all = false;
		}
	}
	if (all) {
		cout << 4999999 << endl;
		return 0;
	}

	int left = 1, right = 1;
	int ans = -1;
	while (right != prime_number.size()) {

		if (bunkai(prime_number[right])) {
			ok_judge(prime_number[right]);
			right++;
		} else {

	/*			cout << left << " " << right << endl;
			 cout << prime_number[left] << " " << prime_number[right]
			 << endl;
			 cout << (prime_number[right] - 1) - (prime_number[left - 1] + 1)
			 << endl;
			 cout << endl;*/
			bool o__L = true;
			for (int i = 0; i < 10; i++) {
				/*cout<<judge[i]<<ok[i]<<endl;*/
				if (judge[i] != ok[i]) {
					o__L = false;
				}
			}
			if (o__L) {
				if((prime_number[right] - 1)
						- (prime_number[left - 1] + 1) ==0){
					ans = max(ans,1);
				}else {


				ans = max(ans,
						(prime_number[right] - 1)
								- (prime_number[left - 1] + 1));
				}
			}
			o__L = false;

			inite_ok();
			right++;
			left = right;
		}
	}

	cout << ans << endl;

	return 0;
}
0