結果

問題 No.12 限定された素数
ユーザー ttkkggww
提出日時 2022-03-08 23:22:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 201 ms / 5,000 ms
コード長 1,155 bytes
コンパイル時間 3,731 ms
コンパイル使用メモリ 253,056 KB
最終ジャッジ日時 2025-01-28 07:49:53
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
ll n;
vector<int> a;

vector<long long> primes(long long n){
	vector<long long> res;
	vector<long long> is(n);
	is[0] = 1;
	is[1] = 1;
	for(int i = 2;i<n;i++){
		if(is[i]==0){
			for(int j = i*2;j<n;j+=i){
				is[j] = 1;
			}
		}
	}
	for(int i = 2;i<n;i++){
		if(is[i]==0){
			res.push_back(i);
		}
	}
	return is;
}

int f(int x){
	int res = 0;
	while(x){
		int y = x%10;
		x/=10;
		res |= (1<<y);
	}
	return res;
}

void solve(){
	auto v = primes(5000001);
	vector<int> fv;
	for(int i = 0;i<5000001;i++){
		if(v[i]==0){
			fv.push_back(f(i));
		}else{
			fv.push_back(0);
		}
	}
	int y = 0;
	for(int i = 0;i<n;i++)y |= 1<<a[i];
	int l = 0;
	int x = 0;
	int ans = -1;
	v.push_back(0);
	fv.push_back(1<<12);
	for(int i = 1;i<=5000001;i++){
		if((~y)&fv[i]){
			if(x==y){
				if(ans<l){
				}
				ans = max(l,ans);
			}
			x = 0;
			l = 0;
		}else{
			x |= fv[i];
			l++;
		}
	}
	cout<<max(-1,ans-1)<<endl;
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n;
	a = vector<int>(n);
	for(auto &i:a)cin >> i;
	solve();
}
0