結果

問題 No.12 限定された素数
ユーザー firiexpfiriexp
提出日時 2019-10-19 16:19:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 2,214 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 99,860 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2023-08-16 02:06:25
合計ジャッジ時間 3,631 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
7,684 KB
testcase_01 AC 56 ms
7,628 KB
testcase_02 AC 62 ms
7,544 KB
testcase_03 AC 53 ms
5,960 KB
testcase_04 AC 59 ms
7,628 KB
testcase_05 AC 56 ms
7,564 KB
testcase_06 AC 54 ms
7,552 KB
testcase_07 AC 53 ms
6,348 KB
testcase_08 AC 56 ms
7,528 KB
testcase_09 AC 54 ms
7,668 KB
testcase_10 AC 55 ms
7,468 KB
testcase_11 AC 52 ms
6,092 KB
testcase_12 AC 53 ms
6,232 KB
testcase_13 AC 54 ms
7,452 KB
testcase_14 AC 57 ms
7,484 KB
testcase_15 AC 59 ms
7,616 KB
testcase_16 AC 55 ms
6,212 KB
testcase_17 AC 57 ms
7,808 KB
testcase_18 AC 55 ms
7,604 KB
testcase_19 AC 56 ms
7,552 KB
testcase_20 AC 54 ms
7,680 KB
testcase_21 AC 56 ms
7,696 KB
testcase_22 AC 57 ms
7,680 KB
testcase_23 AC 57 ms
7,456 KB
testcase_24 AC 57 ms
7,496 KB
testcase_25 AC 57 ms
7,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <limits>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n){
    if(n <= 1) return vector<int>();
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; ++i) {
        if(is_prime[i]) prime.emplace_back(i);
        for (auto &&j : prime){
            if(i*j > n) break;
            is_prime[i*j] = false;
            if(i % j == 0) break;
        }
    }
    return prime;
}


template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "{";
    for (int i = 0; i < v.size(); ++i) {
        if(i) os << ", ";
        os << v[i];
    }
    return os << "}";
}

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
    return os << "{" << p.first << ", " << p.second << "}";
}


int main() {
    int n;
    cin >> n;
    vector<int> num(10);
    for (int i = 0; i < n; ++i) {
        int x; cin >> x;
        num[x] = 1;
    }
    auto v = get_prime(5000000);
    n = v.size();
    vector<int> ngs;
    ngs.emplace_back(-1);
    for (int i = 0; i < n; ++i) {
        int x = v[i];
        int ok = 1;
        while(x){
            if(!num[x%10]) ok = 0;
            x /= 10;
        }
        if(!ok) ngs.emplace_back(i);
    }
    ngs.emplace_back(n);
    int ans = 0;
    for (int i = 0; i+1 < ngs.size(); ++i) {
        vector<int> cnt(10);
        for (int j = ngs[i]+1; j < ngs[i+1]; ++j) {
            int x = v[j];
            while(x){
                cnt[x%10]++;
                x /= 10;
            }
        }
        int ok = 1;
        for (int j = 0; j < 10; ++j) {
            if(num[j] && !cnt[j]) ok = 0;
        }
        int l = (ngs[i] == -1 ? 1 : v[ngs[i]]+1);
        int r = (ngs[i+1] == n ? 5000000: v[ngs[i+1]]-1);
        if(ok){
            ans = max(ans, r-l);
        }
    }
    cout << (ans == 0 ? -1 : ans) << "\n";
    return 0;
}
0