結果
問題 |
No.12 限定された素数
|
ユーザー |
|
提出日時 | 2025-06-08 21:41:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 32 ms / 5,000 ms |
コード長 | 1,269 bytes |
コンパイル時間 | 2,012 ms |
コンパイル使用メモリ | 201,080 KB |
実行使用メモリ | 16,504 KB |
最終ジャッジ日時 | 2025-06-08 21:41:19 |
合計ジャッジ時間 | 4,050 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 5e6+1; bool isp[N]; void init(){ isp[2]=true; for(int i=3;i<N;i+=2) isp[i]=true; for(ll i=3;i<N;i+=2){ if(i*i>=N) break; if(!isp[i]) continue; for(auto j=i*i; j<N;j+=i+i) isp[j]=false; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); init(); auto num_to_set = [&] (ll x){ ll ret = 0; while(x){ ret |= 1<<(x%10); x/=10; } return ret; }; int N; cin>>N; ll full=0; for(int i=0;i<N;++i){ ll x;cin>>x; full|=1<<x; } using P = pair<ll, ll>; vector<P> ok; vector<ll> ng; for(int i=2;i<::N;++i){ if(!isp[i]) continue; auto se = num_to_set(i); if(se & (~full)) ng.emplace_back(i); else ok.emplace_back(i, se); } ng.emplace_back(::N); ok.emplace_back(::N, 0); ll L=1,i=0; ll ans=-1; for(auto&x:ng){ ll R=x-1; ll se=0; while(true){ auto [p, s] = ok[i]; if(p>R) break; se|=s; i+=1; } if(se==full&&ans<R-L) ans=R-L; L=x+1; } cout<<ans<<endl; return 0; }