結果

問題 No.12 限定された素数
コンテスト
ユーザー firiexp
提出日時 2019-10-19 16:19:48
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,214 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 617 ms
コンパイル使用メモリ 100,848 KB
最終ジャッジ日時 2026-05-18 11:04:33
合計ジャッジ時間 1,305 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:15:13: error: 'uint32_t' does not name a type
   15 | using u32 = uint32_t;
      |             ^~~~~~~~
main.cpp:12:1: note: 'uint32_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
   11 | #include <limits>
  +++ |+#include <cstdint>
   12 | 

ソースコード

diff #
raw source code

#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