結果

問題 No.12 限定された素数
ユーザー Ricky_ponRicky_pon
提出日時 2020-05-18 16:22:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 5,000 ms
コード長 2,052 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 196,376 KB
実行使用メモリ 8,788 KB
最終ジャッジ日時 2024-04-09 21:29:41
合計ジャッジ時間 4,263 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
8,708 KB
testcase_01 AC 48 ms
8,636 KB
testcase_02 AC 45 ms
8,592 KB
testcase_03 AC 48 ms
8,780 KB
testcase_04 AC 45 ms
8,584 KB
testcase_05 AC 47 ms
8,552 KB
testcase_06 AC 47 ms
8,428 KB
testcase_07 AC 49 ms
8,480 KB
testcase_08 AC 45 ms
8,652 KB
testcase_09 AC 49 ms
8,580 KB
testcase_10 AC 46 ms
8,660 KB
testcase_11 AC 48 ms
8,580 KB
testcase_12 AC 47 ms
8,544 KB
testcase_13 AC 46 ms
8,572 KB
testcase_14 AC 44 ms
8,652 KB
testcase_15 AC 47 ms
8,476 KB
testcase_16 AC 50 ms
8,684 KB
testcase_17 AC 50 ms
8,464 KB
testcase_18 AC 44 ms
8,736 KB
testcase_19 AC 43 ms
8,788 KB
testcase_20 AC 44 ms
8,636 KB
testcase_21 AC 45 ms
8,640 KB
testcase_22 AC 44 ms
8,716 KB
testcase_23 AC 49 ms
8,576 KB
testcase_24 AC 50 ms
8,652 KB
testcase_25 AC 50 ms
8,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 5000000;

int cnt[10];
bool ok[10], is_prime[MAX+1];

void sieve(){
    fill(is_prime, is_prime+MAX+1, true);
    is_prime[0] = is_prime[1] = false;
    For(i, 2, MAX+1)if(is_prime[i]){
        for(int j=2; i*j<=MAX; ++j) is_prime[i*j] = false;
    }
}

bool check(int n){
    while(n){
        if(!ok[n%10]) return false;
        n /= 10;
    }
    return true;
}

void add(int n){
    while(n){
        ++cnt[n%10];
        n /= 10;
    }
}

int main(){
    sieve();
    int n;
    scanf("%d", &n);
    rep(i, n){
        int a;
        scanf("%d", &a);
        ok[a] = true;
    }
    int ans = 0;
    for(int l=1; l<=MAX;){
        if(is_prime[l] && !check(l)){
            ++l;
            continue;
        }
        int r = l;
        memset(cnt, 0, sizeof(cnt));
        while(r <= MAX){
            if(is_prime[r]){
                if(!check(r)) break;
                add(r);
                ++r;
            }
            else ++r;
        }
        bool flag = true;
        rep(i, 10)if(ok[i] && !cnt[i]){
            flag = false;
            break;
        }
        if(flag) chmax(ans, r-l-1);
        l = r+1;
    }
    printf("%d\n", ans > 0 ? ans : -1);
}
0