結果

問題 No.12 限定された素数
ユーザー xuzijian629xuzijian629
提出日時 2018-11-02 22:01:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 199 ms / 5,000 ms
コード長 1,990 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 209,792 KB
実行使用メモリ 47,448 KB
最終ジャッジ日時 2023-08-16 00:55:35
合計ジャッジ時間 7,944 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
47,428 KB
testcase_01 AC 157 ms
46,444 KB
testcase_02 AC 147 ms
47,016 KB
testcase_03 AC 185 ms
47,084 KB
testcase_04 AC 151 ms
46,668 KB
testcase_05 AC 170 ms
46,720 KB
testcase_06 AC 183 ms
46,988 KB
testcase_07 AC 183 ms
46,984 KB
testcase_08 AC 161 ms
47,280 KB
testcase_09 AC 160 ms
47,328 KB
testcase_10 AC 157 ms
46,408 KB
testcase_11 AC 199 ms
47,448 KB
testcase_12 AC 183 ms
46,564 KB
testcase_13 AC 159 ms
47,224 KB
testcase_14 AC 164 ms
46,504 KB
testcase_15 AC 159 ms
46,432 KB
testcase_16 AC 193 ms
46,752 KB
testcase_17 AC 128 ms
46,476 KB
testcase_18 AC 142 ms
47,240 KB
testcase_19 AC 144 ms
47,248 KB
testcase_20 AC 149 ms
46,492 KB
testcase_21 AC 150 ms
46,956 KB
testcase_22 AC 146 ms
47,280 KB
testcase_23 AC 143 ms
46,312 KB
testcase_24 AC 137 ms
46,640 KB
testcase_25 AC 163 ms
46,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

const int nax = 5000001;

vi ps;

int main() {
    ps = vi(nax, 1);
    ps[0] = ps[1] = 0;
    for (int i = 2; i < nax; i++) {
        if (!ps[i]) continue;
        for (int j = 2; i * j < nax; j++) {
            ps[i * j] = 0;
        }
    }

    vi qs;
    for (int i = 0; i < nax; i++) {
        if (ps[i]) qs.push_back(i);
    }

    int n;
    cin >> n;
    set<int> use;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        use.insert(a);
    }
    set<int> ss;

    int l = 0, r = 0;
    i64 width = -1;
    while (1) {
        if (use != ss) {
            if (r == qs.size()) break;
            int q = qs[r];
            int ok = 1;
            while (q) {
                if (use.count(q % 10) == 0) {
                    ok = 0;
                }
                q /= 10;
            }
            if (ok) {
                q = qs[r];
                while (q) {
                    ss.insert(q % 10);
                    q /= 10;
                }
                if (use == ss) {
                    width = max(width, ((r + 1 == qs.size() ? nax : qs[r + 1]) - 1) - ((l == 0 ? 0 : qs[l - 1]) + 1));
                }
                r++;
            } else {
                l = r + 1;
                r = l;
                ss.clear();
            }
        } else {
            if (r == qs.size()) break;
            int q = qs[r];
            int ok = 1;
            while (q) {
                if (use.count(q % 10) == 0) {
                    ok = 0;
                }
                q /= 10;
            }
            if (ok) {
                width = max(width, ((r + 1 == qs.size() ? nax : qs[r + 1]) - 1) - ((l == 0 ? 0 : qs[l - 1]) + 1));
                r++;
            } else {
                l = r + 1;
                r = l;
                ss.clear();
            }      
        }    
    }
    cout << width << endl;
}
0