結果

問題 No.12 限定された素数
コンテスト
ユーザー hogeover30
提出日時 2015-02-17 15:44:22
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 1,135 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,957 ms
コンパイル使用メモリ 170,796 KB
実行使用メモリ 26,108 KB
最終ジャッジ日時 2026-03-08 16:01:41
合計ジャッジ時間 2,804 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
using namespace std;
const int N=5000001;
int c[N];
int main()
{
    vector<int> primes;
    vector<int> v;
    for(int i=2;i<N;++i) if (!c[i]) {
        primes.push_back(i);
        int x=0;
        for(int t=i;t>0;t/=10) x|=1<<(t%10);
        v.push_back(x);
        for(int j=i+i;j<N;j+=i) c[j]=1;
    }

    int n;
    while (cin>>n) {
        int a=0;
        for(int i=0;i<n;++i) { int t; cin>>t; a|=1<<t; }
        int s=0, e=0, x=v[s];
        int res=-1;
        while (s<v.size() and e<v.size()) {
            if ((x|v[e]|a)>a) {
                if (x==a and s<e) {
                    int L=primes[e]-1;
                    int K=!!s*primes[s-1]+1;
                    res=max(res, L-K);
                }
                s=e+1;
                e=s;
                x=v[e];
            }
            else {
                x|=v[e];
                ++e;
            }
            if (x==a) {
                int L=e<primes.size()?primes[e]-1:5000000;
                int K=!!s*primes[s-1]+1;
                res=max(res, L-K);
            }
        }
        cout<<res<<endl;
    }
}
0