結果

問題 No.12 限定された素数
コンテスト
ユーザー himueu
提出日時 2026-10-16 01:52:09
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 131 ms / 5,000 ms
+ 846µs
コード長 1,497 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,302 ms
コンパイル使用メモリ 181,272 KB
実行使用メモリ 9,796 KB
最終ジャッジ日時 2026-10-16 01:52:21
合計ジャッジ時間 8,511 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main() {
    using namespace std;
    constexpr int m = 5e6;
    vector<bool> is_prime(m + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= m / i; i++) {
        if (!is_prime[i]) { continue; }
        for (int j = i * i; j <= m; j += i) { is_prime[j] = false; }
    }

    int n, req = 0;
    cin >> n;
    while (n--) {
        int a;
        cin >> a;
        req |= 1 << a;
    }

    vector<int> count(10, 0);
    int r = 1, nums = 0, ans = -1;
    for (int l = 1; l <= m; l++) {
        if (r < l) {
            r = l;
            for (int i = 0; i < 10; i++) { count[i] = 0; }
            nums = 0;
        }
        while (r <= m) {
            if (is_prime[r]) {
                bool ng = false;
                for (auto c : to_string(r)) {
                    int nc = c - '0';
                    if (!(req >> nc & 1)) { ng = true; }
                }
                if (ng) { break; }
                for (auto c : to_string(r)) {
                    int nc = c - '0';
                    if (count[nc]++ == 0) { nums |= 1 << nc; }
                }
            }
            r++;
        }
        if (nums == req) { ans = max(ans, r - l - 1); }
        if (is_prime[l]) {
            for (auto c : to_string(l)) {
                int nc = c - '0';
                if (--count[nc] == 0) { nums ^= 1 << nc; }
            }
        }
    }
    cout << ans << endl;
}
0