結果

問題 No.12 限定された素数
ユーザー commy
提出日時 2018-08-12 20:42:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 595 ms / 5,000 ms
コード長 1,931 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 77,344 KB
実行使用メモリ 355,560 KB
最終ジャッジ日時 2024-11-24 10:05:22
合計ジャッジ時間 17,364 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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