結果

問題 No.3127 Multiple of Twin Prime
ユーザー D M
提出日時 2025-05-23 16:00:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 114 ms / 2,500 ms
コード長 1,367 bytes
コンパイル時間 4,637 ms
コンパイル使用メモリ 281,560 KB
実行使用メモリ 15,836 KB
最終ジャッジ日時 2025-05-23 16:00:26
合計ジャッジ時間 7,716 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
const ll MOD = 998244353;

vector<ll> sieve_primes(ll N) {
    vector<bool> is_prime(N+1, true);
    is_prime[0] = is_prime[1] = false;

    ll limit = floor(sqrt(N));
    for (ll i = 2; i <= limit; ++i) {
        if (is_prime[i]) {
            // i が素数なら、その倍数をすべて合成数としてマーク
            for (ll j = i * i; j <= N; j += i) {
                is_prime[j] = false;
            }
        }
    }

    // 素数だけを抜き出す
    vector<ll> primes;
    primes.reserve(N / log(N));  // 素数個数の概算で予約
    for (ll i = 2; i <= N; ++i) {
        if (is_prime[i]) {
            primes.push_back(i);
        }
    }
    return primes;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    auto primes = sieve_primes(10000000);
    vector<ll> P;
    rep(i, primes.size()-1) {
        if(primes[i+1] - primes[i] == 2) {
            P.push_back(primes[i]*primes[i+1]);
        }
    }
    ll t;
    cin >> t;
    while(t--) {
        ll n;
        cin >> n;
        auto it = upper_bound(all(P), n);
        if(it==P.begin()){
            cout << -1 << '\n';
            continue;
        }
        cout << *prev(it) << '\n';
    }
}
0