結果

問題 No.12 限定された素数
ユーザー btkbtk
提出日時 2015-05-03 19:25:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 1,555 bytes
コンパイル時間 725 ms
コンパイル使用メモリ 88,836 KB
実行使用メモリ 14,688 KB
最終ジャッジ日時 2023-08-15 22:59:09
合計ジャッジ時間 3,700 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
14,548 KB
testcase_01 AC 70 ms
14,416 KB
testcase_02 AC 70 ms
14,416 KB
testcase_03 AC 59 ms
14,552 KB
testcase_04 AC 70 ms
14,556 KB
testcase_05 AC 71 ms
14,356 KB
testcase_06 AC 70 ms
14,560 KB
testcase_07 AC 70 ms
14,352 KB
testcase_08 AC 71 ms
14,412 KB
testcase_09 AC 72 ms
14,572 KB
testcase_10 AC 72 ms
14,408 KB
testcase_11 AC 70 ms
14,356 KB
testcase_12 AC 70 ms
14,552 KB
testcase_13 AC 72 ms
14,688 KB
testcase_14 AC 71 ms
14,352 KB
testcase_15 AC 70 ms
14,496 KB
testcase_16 AC 72 ms
14,412 KB
testcase_17 AC 70 ms
14,424 KB
testcase_18 AC 71 ms
14,544 KB
testcase_19 AC 70 ms
14,360 KB
testcase_20 AC 70 ms
14,408 KB
testcase_21 AC 71 ms
14,416 KB
testcase_22 AC 70 ms
14,364 KB
testcase_23 AC 70 ms
14,484 KB
testcase_24 AC 71 ms
14,404 KB
testcase_25 AC 72 ms
14,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
bool isprime[5000001];
vector<int>prime;
bool num[500000][10] = { { false } };
int main(void){
	bool ans[10] = { false };
	int N,m;
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		cin >> m;
		ans[m] = true;
	}
	for (int i = 2; i <= 5000000;i++)isprime[i] = true;
	for (int i = 2; i <= 5000000; i++){
		if (isprime[i]){
			prime.push_back(i);
			for (int j = i * 2; j <= 5000000; j += i)isprime[j] = false;
		}
	}
	for (int k = 0; k < (int)prime.size();k++){
		int q = prime[k];
		while (q){
			num[k][q % 10] = true;
			q /= 10;
		}
		
	}
	int ml=0, mr=-1;
	int l=0;
	bool cnt[10] = {false};
	for (int r = 0; r < (int)prime.size(); r++){
		bool f = true;
		bool g = true;
		for (int i = 0; i < 10; i++){
			cnt[i] |= num[r][i];
			if (ans[i] != cnt[i])g = false;
			if (ans[i] == false && cnt[i] == true)f = false;
		}
		if (f == false){
			for (int i = 0; i < 10; i++)
				cnt[i] = false;
			l = r + 1;
			continue;
		}
		
		if (g == true){
			int R = 5000000;
			if (r + 1 < (int)prime.size())R = prime[r + 1] - 1;
			int L = 1;
			if (l > 0)L = prime[l - 1] + 1;
			if (mr - ml < R - L)
			{
				mr = R, ml = L;
			//	cout << ml << " " << mr << " " << mr - ml << endl;
			}

		}
		
	}
	cout << mr - ml << endl;;
	return 0;
}
0