結果

問題 No.12 限定された素数
ユーザー PachicobuePachicobue
提出日時 2017-05-29 02:51:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,027 bytes
コンパイル時間 1,502 ms
コンパイル使用メモリ 167,784 KB
実行使用メモリ 207,416 KB
最終ジャッジ日時 2023-09-07 18:54:25
合計ジャッジ時間 39,138 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,537 ms
206,060 KB
testcase_01 AC 1,358 ms
206,196 KB
testcase_02 AC 1,699 ms
207,276 KB
testcase_03 AC 130 ms
206,068 KB
testcase_04 AC 1,476 ms
206,692 KB
testcase_05 AC 1,282 ms
206,528 KB
testcase_06 AC 1,132 ms
206,484 KB
testcase_07 AC 1,257 ms
205,876 KB
testcase_08 AC 1,177 ms
207,220 KB
testcase_09 AC 1,291 ms
206,040 KB
testcase_10 WA -
testcase_11 AC 1,142 ms
206,040 KB
testcase_12 AC 1,332 ms
206,108 KB
testcase_13 AC 1,359 ms
206,068 KB
testcase_14 AC 1,234 ms
205,804 KB
testcase_15 AC 1,367 ms
206,104 KB
testcase_16 AC 1,162 ms
206,260 KB
testcase_17 AC 1,529 ms
205,912 KB
testcase_18 AC 1,526 ms
205,856 KB
testcase_19 AC 1,524 ms
205,800 KB
testcase_20 AC 1,270 ms
207,124 KB
testcase_21 AC 1,229 ms
205,724 KB
testcase_22 AC 1,594 ms
205,968 KB
testcase_23 AC 1,607 ms
206,020 KB
testcase_24 AC 1,429 ms
206,540 KB
testcase_25 AC 1,344 ms
207,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p)
{
    return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc)
{
    o << "sz = " << vc.size() << endl
      << "[";
    for (const T& v : vc)
        o << v << ",";
    o << "]";
    return o;
}
using ll = long long;
constexpr ll MOD = 1000000007;

constexpr ll MAX = 5000000;
bool isprime[MAX + 1];
int digits[MAX + 1][10];
vector<ll> prime;
bool allowed[10];

bool ok(ll interval)
{
    rep1(i, MAX - interval)
    {
        bool flag = true;
        rep(j, 10)
        {
            if (allowed[j]) {
                if (digits[i + interval][j] - digits[i - 1][j] <= 0) {
                    flag = false;
                }
            } else {
                if (digits[i + interval][j] - digits[i - 1][j] > 0) {
                    flag = false;
                }
            }
        }
        if (flag) {
            return true;
        }
    }
    return false;
}

int main()
{
    for (ll i = 2; i <= MAX; i++) {
        isprime[i] = true;
    }

    for (ll i = 2; i <= MAX; i++) {
        if (isprime[i]) {
            for (ll j = 2; i * j <= MAX; j++) {
                isprime[i * j] = false;
            }
        }
    }
    rep(i, 10)
    {
        digits[1][i] = 0;
    }
    for (ll i = 2; i <= MAX; i++) {
        rep(j, 10)
        {
            digits[i][j] = digits[i - 1][j];
        }
        if (isprime[i]) {
            prime.pb(i);
            bool digit[10];
            rep(k, 10)
            {
                digit[k] = false;
            }
            ll p = i;
            while (p > 0) {
                digit[p % 10] = true;
                p /= 10;
            }
            rep(k, 10)
            {
                if (digit[k]) {
                    digits[i][k]++;
                }
            }
        }
    }

    int n;
    cin >> n;
    rep(i, n)
    {
        int a;
        cin >> a;
        allowed[a] = true;
    }

    ll inf = 1;
    ll sup = MAX - 1;
    if (ok(sup)) {
        cout << sup << endl;
        return 0;
    }
    while (inf < sup) {
        const ll mid = (inf + sup) / 2;
        if (ok(mid)) {
            if (inf == mid) {
                break;
            }
            inf = mid;
        } else {
            sup = mid;
        }
    }
    if (ok(inf)) {
        cout << inf << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}
0