結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
17,980 KB
testcase_01 AC 42 ms
17,988 KB
testcase_02 AC 42 ms
18,140 KB
testcase_03 AC 44 ms
17,996 KB
testcase_04 AC 41 ms
17,976 KB
testcase_05 AC 42 ms
18,080 KB
testcase_06 AC 42 ms
18,100 KB
testcase_07 AC 44 ms
18,024 KB
testcase_08 AC 43 ms
18,176 KB
testcase_09 AC 42 ms
18,036 KB
testcase_10 AC 43 ms
18,120 KB
testcase_11 AC 45 ms
18,296 KB
testcase_12 AC 44 ms
17,936 KB
testcase_13 AC 42 ms
18,160 KB
testcase_14 AC 43 ms
18,300 KB
testcase_15 AC 43 ms
18,156 KB
testcase_16 AC 44 ms
18,180 KB
testcase_17 AC 43 ms
18,112 KB
testcase_18 AC 42 ms
18,100 KB
testcase_19 AC 42 ms
18,280 KB
testcase_20 AC 42 ms
17,972 KB
testcase_21 AC 42 ms
18,036 KB
testcase_22 AC 42 ms
18,140 KB
testcase_23 AC 42 ms
18,056 KB
testcase_24 AC 42 ms
17,940 KB
testcase_25 AC 43 ms
18,140 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){
        each(ng, no)if(sum[ng][l + 1] - sum[ng][l]){
            goto NG;
        }
        smax(r, l);
        while(r<m-1){
            each(ng, no)
                if(sum[ng][r + 1] - sum[ng][l])goto NG2;
            r++;
        }
        NG2:;
        each(u, use){
            if(sum[u][r] - sum[u][l] == 0){
                goto NG;
            }
        }
        smax(ans, (ps[r] - 1) - (ps[l - 1] + 1));
        NG:;
    }
    cout << ans << endl;
}
0