結果

問題 No.12 限定された素数
コンテスト
ユーザー KKT89
提出日時 2026-07-12 01:34:04
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 21 ms / 5,000 ms
+ 801µs
コード長 1,413 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,403 ms
コンパイル使用メモリ 332,748 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-12 01:34:11
合計ジャッジ時間 5,394 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    int target = 0;
    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        target |= 1 << a;
    }

    const int LIM = 5000000;

    vector<bool> isPrime(LIM + 1, true);
    isPrime[0] = isPrime[1] = false;

    for (int i = 2; 1LL * i * i <= LIM; ++i) {
        if (!isPrime[i]) continue;

        for (int j = i * i; j <= LIM; j += i) {
            isPrime[j] = false;
        }
    }

    int ans = -1;

    // 直前の禁止素数
    int lastBad = 0;

    // lastBad より後にある、使用可能な素数の数字集合
    int used = 0;

    for (int p = 2; p <= LIM; ++p) {
        if (!isPrime[p]) continue;

        int mask = 0;
        int x = p;

        while (x > 0) {
            mask |= 1 << (x % 10);
            x /= 10;
        }

        // target に含まれない数字が使われている
        if (mask & ~target) {
            // 区間 [lastBad + 1, p - 1]
            if (used == target) {
                ans = max(ans, p - lastBad - 2);
            }

            lastBad = p;
            used = 0;
        } else {
            used |= mask;
        }
    }

    // 最後の禁止素数から 5000000 まで
    if (used == target) {
        ans = max(ans, LIM - lastBad - 1);
    }

    cout << ans << '\n';
}
0