結果

問題 No.12 限定された素数
ユーザー parukiparuki
提出日時 2016-10-08 14:56:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 2,009 bytes
コンパイル時間 1,751 ms
コンパイル使用メモリ 172,300 KB
実行使用メモリ 18,284 KB
最終ジャッジ日時 2023-08-15 23:35:36
合計ジャッジ時間 4,113 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
18,180 KB
testcase_01 AC 42 ms
17,940 KB
testcase_02 AC 42 ms
17,984 KB
testcase_03 AC 44 ms
17,940 KB
testcase_04 AC 42 ms
18,072 KB
testcase_05 AC 43 ms
17,996 KB
testcase_06 AC 43 ms
18,044 KB
testcase_07 AC 44 ms
17,996 KB
testcase_08 AC 42 ms
18,020 KB
testcase_09 AC 42 ms
18,144 KB
testcase_10 AC 43 ms
18,064 KB
testcase_11 AC 45 ms
17,940 KB
testcase_12 AC 44 ms
18,116 KB
testcase_13 AC 42 ms
18,284 KB
testcase_14 AC 42 ms
18,180 KB
testcase_15 AC 42 ms
17,992 KB
testcase_16 AC 44 ms
18,108 KB
testcase_17 AC 43 ms
17,984 KB
testcase_18 AC 43 ms
17,992 KB
testcase_19 AC 43 ms
17,980 KB
testcase_20 AC 42 ms
18,160 KB
testcase_21 AC 42 ms
17,992 KB
testcase_22 AC 43 ms
17,984 KB
testcase_23 AC 42 ms
18,032 KB
testcase_24 AC 42 ms
17,996 KB
testcase_25 AC 43 ms
17,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

vector<int> sieve(int n){
    vector<bool> f(n+1);
    for(int i=3; i<=n; i+=2) f[i] = 1;
    for(int i=3; i<=sqrt(n); i+=2) if(f[i]) for(int j=i*3; j<=n; j+=i) f[j] = 0;
    vector<int> res{0, 2};
    for(int i=3; i<=n; i+=2) if(f[i]) res.push_back(i);
    res.push_back(n+1);
    return res;
}

const int MA = 5000000, SA = 348515;
int N, A[10], sum[10][SA + 1], flag[10];

int main(){
    cin >> N;
    rep(i, N)cin >> A[i], flag[A[i]] = 1;
    auto ps = sieve(MA);
    int m = sz(ps);
    rep(i, m){
        int p = ps[i];
        while(p > 0){
            sum[p % 10][i+1]++;
            p /= 10;
        }
    }
    rep(i, 10)rep(j, SA)sum[i][j + 1] += sum[i][j];

    vi no, use;
    rep(i, 10)(flag[i] ? use : no).push_back(i);
    int r = 1, ans = -1;
    for(int l = 1; l < m; ++l){
        int okok = 1;
        each(ng, no)if(sum[ng][l + 1] - sum[ng][l]){
            okok = 0;
            break;
        }
        if(!okok)continue;
        smax(r, l);
        while(r<m-1){
            int ok = 1;
            each(ng, no){
                if(sum[ng][r+1] - sum[ng][l]){
                    ok = 0;
                    break;
                }
            }
            if(!ok)break;
            else r++;
        }
        int OK = 1;
        each(u, use){
            if(sum[u][r] - sum[u][l] == 0){
                OK = 0;
                break;
            }
        }
        if(OK){
            smax(ans, (ps[r] - 1) - (ps[l - 1] + 1));
        }
    }
    cout << ans << endl;
}
0