結果

問題 No.12 限定された素数
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-03 18:35:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 173 ms / 5,000 ms
コード長 1,374 bytes
コンパイル時間 1,772 ms
コンパイル使用メモリ 170,624 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 23:40:48
合計ジャッジ時間 7,422 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
4,376 KB
testcase_01 AC 170 ms
4,380 KB
testcase_02 AC 165 ms
4,376 KB
testcase_03 AC 136 ms
4,376 KB
testcase_04 AC 167 ms
4,376 KB
testcase_05 AC 161 ms
4,376 KB
testcase_06 AC 160 ms
4,376 KB
testcase_07 AC 163 ms
4,376 KB
testcase_08 AC 165 ms
4,380 KB
testcase_09 AC 166 ms
4,376 KB
testcase_10 AC 163 ms
4,376 KB
testcase_11 AC 153 ms
4,380 KB
testcase_12 AC 156 ms
4,376 KB
testcase_13 AC 162 ms
4,376 KB
testcase_14 AC 165 ms
4,376 KB
testcase_15 AC 162 ms
4,376 KB
testcase_16 AC 158 ms
4,380 KB
testcase_17 AC 171 ms
4,376 KB
testcase_18 AC 172 ms
4,380 KB
testcase_19 AC 172 ms
4,376 KB
testcase_20 AC 165 ms
4,376 KB
testcase_21 AC 168 ms
4,380 KB
testcase_22 AC 169 ms
4,376 KB
testcase_23 AC 169 ms
4,376 KB
testcase_24 AC 171 ms
4,376 KB
testcase_25 AC 163 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

std::vector<bool> IsPrime;
void eratosthenes(size_t max_n) {
    if (max_n + 1 > IsPrime.size()) {
        IsPrime.resize(max_n + 1, true);
    }
    IsPrime[0] = false;
    IsPrime[1] = false;

    for (size_t i = 2; i * i <= max_n; i++)
        if (IsPrime[i])
            for (size_t j = 2; i * j <= max_n; j++)
                IsPrime[i * j] = false;
}

int MAX = 5000000;


int main() {
    int N, A[11] = {};
    cin >> N;
    while (N--) {
        int a;
        cin >> a;
        A[a] = 1;
    }

    int used[11] = {};
    auto use = [&](int p, int delta) {
        while (p) {
            used[p % 10] += delta;
            p /= 10;
        }
    };

    eratosthenes(MAX + 10);

    int ans = -1;
    int k = 1, l = 1;
    while (l <= MAX) {
        if (IsPrime[l]) {
            use(l, 1);
        }

        for (int d = 0; d < 10; d++) {
            if (!A[d]) {
                while (used[d]) {
                    if (IsPrime[k]) {
                        use(k, -1);
                    }
                    k++;
                }
            }
        }

        bool flag = true;
        for (int d = 0; d < 10; d++) {
            if (A[d] && !used[d])
                flag = false;
        }

        if (flag) {
            ans = max(ans, l - k);
        }

        l++;
    }
    cout << ans << endl;
}
0