結果

問題 No.12 限定された素数
ユーザー commycommy
提出日時 2018-08-12 20:42:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 609 ms / 5,000 ms
コード長 1,931 bytes
コンパイル時間 6,064 ms
コンパイル使用メモリ 76,332 KB
実行使用メモリ 355,336 KB
最終ジャッジ日時 2023-08-16 00:52:15
合計ジャッジ時間 18,039 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 593 ms
355,252 KB
testcase_01 AC 593 ms
355,080 KB
testcase_02 AC 589 ms
355,024 KB
testcase_03 AC 568 ms
355,072 KB
testcase_04 AC 605 ms
355,208 KB
testcase_05 AC 590 ms
355,112 KB
testcase_06 AC 575 ms
355,332 KB
testcase_07 AC 585 ms
355,144 KB
testcase_08 AC 581 ms
355,204 KB
testcase_09 AC 591 ms
355,028 KB
testcase_10 AC 594 ms
355,176 KB
testcase_11 AC 571 ms
355,112 KB
testcase_12 AC 582 ms
355,124 KB
testcase_13 AC 578 ms
355,064 KB
testcase_14 AC 593 ms
355,084 KB
testcase_15 AC 577 ms
355,116 KB
testcase_16 AC 587 ms
355,336 KB
testcase_17 AC 603 ms
355,116 KB
testcase_18 AC 608 ms
355,312 KB
testcase_19 AC 595 ms
355,020 KB
testcase_20 AC 602 ms
355,132 KB
testcase_21 AC 587 ms
355,208 KB
testcase_22 AC 590 ms
355,252 KB
testcase_23 AC 592 ms
355,208 KB
testcase_24 AC 588 ms
355,064 KB
testcase_25 AC 609 ms
355,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
    int N;
    cin >> N;
    const int P = 5000001;
    //const int P = 51;
    vector<bool> isPrime(P, true);
    isPrime[0] = isPrime[1] = false;
    for (int i = 2; i * i < P; i++) {
        if (isPrime[i]) {
            for (int j = 2 * i; j < P; j += i) {
                isPrime[j] = false;
            }
        }
    }
    auto Pd = make_v(P, 10, 0);
    REP(i, 0, P) {
        if (isPrime[i]) {
            int n = i;
            while (n) {
                Pd[i][n % 10]++;
                n /= 10;
            }
        }
    }
    REP(i, 1, P) {
        REP(j, 0, 10) {
            Pd[i][j] += Pd[i - 1][j];
        }
    }
    int ans = -1;
    vector<bool> A(10, false);
    REP(i, 0, N) {
        int a;
        cin >> a;
        A[a] = true;
    }

    int l = 0;
    while (l < P) {
        int r = l + 1;
        bool isPm = false;
        while (r < P) {
            bool ok = true;
            REP(i, 0, 10) {
                if (!A[i]) {
                    ok &= (Pd[r][i] == Pd[l][i]);
                }
            }
            if (ok) {
                isPm |= isPrime[r];
                r++;
            } else {
                break;
            }
        }
        REP(i, 0, 10) {
            if (A[i]) {
                isPm &= (Pd[r - 1][i] > Pd[l][i]);
            }
        }
        if (isPm) {
            ans = max(ans, r - l - 2);
        }
        l = r;
    }
    cout << ans << endl;
    return 0;
}
0