結果

問題 No.3127 Multiple of Twin Prime
ユーザー rgnerdplayer
提出日時 2025-05-01 15:57:31
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 125 ms / 2,500 ms
コード長 1,066 bytes
コンパイル時間 3,034 ms
コンパイル使用メモリ 279,344 KB
実行使用メモリ 46,588 KB
最終ジャッジ日時 2025-05-01 15:57:40
合計ジャッジ時間 6,315 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using i64 = long long;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    constexpr int V = 1e7 + 10;
    vector<int> minp(V + 1), primes;
    for (int i = 2; i <= V; i++) {
        if (!minp[i]) {
            primes.push_back(i);
            minp[i] = i;
        }
        for (int p : primes) {
            if (p > V / i) {
                break;
            }
            minp[p * i] = p;
            if (i % p == 0) {
                break;
            }
        }
    }

    vector<i64> a;

    for (int i = 0; i + 1 < primes.size(); i++) {
        if (primes[i + 1] == primes[i] + 2) {
            a.push_back(1LL * primes[i] * primes[i + 1]);
        }
    }

    auto solve = [&]() {
        i64 n;
        cin >> n;

        int i = upper_bound(a.begin(), a.end(), n) - a.begin();
        if (i == 0) {
            cout << -1 << '\n';
        } else {
            cout << a[i - 1] << '\n';
        }
    };

    int t;
    cin >> t;

    while (t--) {
        solve();
    }
    
    return 0;
}
0