結果
| 問題 |
No.12 限定された素数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-08-04 15:03:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 109 ms / 5,000 ms |
| コード長 | 1,447 bytes |
| コンパイル時間 | 1,962 ms |
| コンパイル使用メモリ | 204,800 KB |
| 最終ジャッジ日時 | 2025-02-23 20:59:06 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int Need = 5000000;
vector<int> allp;
vector<bool> prime(Need+1,true);
prime.at(0) = false; prime.at(1) = false;
for(int i=2; i<=Need; i++){
if(!prime.at(i)) continue;
allp.push_back(i);
for(long long k=((long long)i)*i; k<=Need; k+=i) prime.at(k) = false;
}
int n = allp.size();
vector<int> use(n);
for(int i=0; i<n; i++){
int p = allp.at(i);
int now = 0;
while(p) now |= (1<<(p%10)),p /= 10;
use.at(i) = now;
}
vector<vector<int>> next(n,vector<int>(10,n));
for(int i=n-1; i>=0; i--){
for(int k=0; k<10; k++){
if(use.at(i)&(1<<k)) next.at(i).at(k) = i;
else if(i != n-1) next.at(i).at(k) = next.at(i+1).at(k);
}
}
int N; cin >> N;
int A = 0;
while(N--){
int a; cin >> a;
A += 1<<a;
}
allp.push_back(5000001);
int answer = -1;
for(int i=0; i<n; i++){
int one = -1,two = n;
for(int k=0; k<10; k++){
if(A&(1<<k)) one = max(one,next.at(i).at(k));
else two = min(two,next.at(i).at(k));
}
if(one >= two) continue;
if(i == 0) answer = max(answer,allp.at(two)-1-1);
else answer = max(answer,allp.at(two)-1-(allp.at(i-1)+1));
}
cout << answer << endl;
}