結果

問題 No.12 限定された素数
ユーザー fiordfiord
提出日時 2015-07-12 19:14:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 959 bytes
コンパイル時間 1,388 ms
コンパイル使用メモリ 147,752 KB
実行使用メモリ 10,192 KB
最終ジャッジ日時 2023-08-15 23:01:31
合計ジャッジ時間 4,315 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
10,000 KB
testcase_01 AC 63 ms
10,088 KB
testcase_02 AC 63 ms
10,076 KB
testcase_03 AC 59 ms
9,944 KB
testcase_04 AC 64 ms
10,072 KB
testcase_05 AC 65 ms
10,084 KB
testcase_06 AC 65 ms
9,940 KB
testcase_07 AC 65 ms
10,148 KB
testcase_08 AC 65 ms
10,008 KB
testcase_09 AC 65 ms
9,948 KB
testcase_10 AC 66 ms
10,152 KB
testcase_11 AC 63 ms
9,944 KB
testcase_12 AC 63 ms
10,140 KB
testcase_13 AC 61 ms
10,144 KB
testcase_14 AC 62 ms
10,140 KB
testcase_15 AC 63 ms
10,068 KB
testcase_16 AC 63 ms
10,080 KB
testcase_17 AC 64 ms
10,140 KB
testcase_18 AC 65 ms
10,152 KB
testcase_19 AC 63 ms
9,948 KB
testcase_20 AC 62 ms
10,000 KB
testcase_21 AC 64 ms
10,000 KB
testcase_22 AC 64 ms
10,016 KB
testcase_23 AC 62 ms
9,996 KB
testcase_24 AC 63 ms
10,192 KB
testcase_25 AC 62 ms
9,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define E 5000000
vector<int> cnt(10,0);

void change(int x,int cha){
	while(x>0){
		cnt[x%10]+=cha;
		x/=10;
	}
}

int main(){
	int n;	cin>>n;
	bool a[10];
	for(int i=0;i<10;i++)	a[i]=false;
	for(int i=0;i<n;i++){
		int tem;	cin>>tem;
		a[tem]=true;
	}
	vector<int> prime;
	bool sieve[E+1];
	for(int i=0;i<=E;i++)	sieve[i]=true;
	for(int i=2;i<=E;i++){
		if(sieve[i]){
			prime.push_back(i);
			for(int j=i*2;j<=E;j+=i)	sieve[j]=false;
		}
	}
	int ans=-1,end=0;
	for(int i=0;i<(int)prime.size();i++){
		change(prime[i],1);
		for(int j=0;j<10;j++){
			if(a[j])	continue;
			while(cnt[j]>0){
				change(prime[end],-1);
				end++;
			}
		}
		bool flag=true;
		for(int j=0;j<10;j++){
			if(a[j]&&cnt[j]==0)	flag=false;
		}
		if(flag){
			int k,l;
			if(end!=0)	k=prime[end-1]+1;
			else 	k=1;
			if(i+1!=(int)prime.size())	l=prime[i+1]-1;
			else 	l=E;
			ans=max(ans,l-k);
		}
	}
	cout<<ans<<endl;
	return 0;
}
			
0