結果

問題 No.12 限定された素数
ユーザー kimiyukikimiyuki
提出日時 2017-01-10 17:28:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 1,429 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 70,236 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-15 23:36:31
合計ジャッジ時間 4,596 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
4,380 KB
testcase_01 AC 104 ms
4,380 KB
testcase_02 AC 97 ms
4,384 KB
testcase_03 AC 102 ms
4,380 KB
testcase_04 AC 96 ms
4,376 KB
testcase_05 AC 103 ms
4,380 KB
testcase_06 AC 99 ms
4,380 KB
testcase_07 AC 100 ms
4,376 KB
testcase_08 AC 95 ms
4,376 KB
testcase_09 AC 98 ms
4,380 KB
testcase_10 AC 96 ms
4,380 KB
testcase_11 AC 108 ms
4,376 KB
testcase_12 AC 98 ms
4,380 KB
testcase_13 AC 101 ms
4,380 KB
testcase_14 AC 100 ms
4,376 KB
testcase_15 AC 101 ms
4,380 KB
testcase_16 AC 95 ms
4,376 KB
testcase_17 AC 137 ms
4,384 KB
testcase_18 AC 102 ms
4,380 KB
testcase_19 AC 108 ms
4,380 KB
testcase_20 AC 96 ms
4,380 KB
testcase_21 AC 104 ms
4,388 KB
testcase_22 AC 113 ms
4,380 KB
testcase_23 AC 112 ms
4,380 KB
testcase_24 AC 131 ms
4,380 KB
testcase_25 AC 111 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <array>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using ll = long long;
using namespace std;
template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }
const int limit = 5000000;
vector<bool> sieve_of_eratosthenes(int n) { // enumerate primes in [2,n] with O(n log log n)
    vector<bool> is_prime(n+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i*(ll)i <= n; ++i)
        if (is_prime[i])
            for (int k = i+i; k <= n; k += i)
                is_prime[k] = false;
    return is_prime;
}
int main() {
    vector<bool> is_prime = sieve_of_eratosthenes(limit);
    int n; cin >> n;
    array<bool, 10> a = {};
    repeat (i,n) { int d; cin >> d; a[d] = true; }
    array<int, 10> used = {};
    auto use = [&](int p, int delta) {
        while (p) {
            used[p % 10] += delta;
            p /= 10;
        }
    };
    int ans = -1;
    for (int k = 1, l = 1; l <= limit; ++ l) {
        if (is_prime[l]) use(l, +1);
        repeat (d,10) if (not a[d]) while (used[d]) {
            if (is_prime[k]) use(k, -1);
            ++ k;
        }
        bool is_fulfilled = true;
        repeat (d,10) {
            if (a[d] and not used[d]) {
                is_fulfilled = false;
                break;
            }
        }
        if (is_fulfilled) setmax(ans, l - k);
    }
    cout << ans << endl;
    return 0;
}
0