結果

問題 No.3127 Multiple of Twin Prime
ユーザー ldsyb
提出日時 2025-04-25 22:28:16
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,023 bytes
コンパイル時間 6,083 ms
コンパイル使用メモリ 334,268 KB
実行使用メモリ 20,560 KB
最終ジャッジ日時 2025-04-25 22:28:34
合計ジャッジ時間 13,721 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample TLE * 1
other -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif

template <typename T>
T gcd(T a, T b)
{
    while (b)
    {
        T tmp = b;
        b = a % b;
        a = tmp;
    }
    return a;
}

template <typename T>
T pow_mod(T a, T n, T mod)
{
    T r = 1;
    for (; n; n >>= 1)
    {
        if (n & 1)
        {
            (r *= a) %= mod;
        }
        (a *= a) %= mod;
    }
    return r;
}

template <typename T>
bool miller_rabin(T n)
{
    if (n < 2)
    {
        return false;
    }

    for (T a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022})
    {
        if (gcd(a, n) != 1)
        {
            continue;
        }
        T m = n - 1;
        if (pow_mod(a, m, n) != 1)
        {
            return false;
        }
        for (m >>= 1; m % 2 == 0; m >>= 1)
        {
            T puni = pow_mod(a, m, n);
            if (puni == n - 1)
            {
                break;
            }
            if (puni != 1)
            {
                return false;
            }
        }
        T puni = pow_mod(a, m, n);
        if ((puni != n - 1) && (puni != 1))
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int64_t T;
    cin >> T;

    vector<bool> is_prime(10000010, false);
    is_prime[2] = true;
    for (int64_t i = 3; i <= 10000009; i += 2)
    {
        is_prime[i] = miller_rabin(i);
    }

    vector<int64_t> ls;
    for (int64_t i = 3; i <= 10000000; i += 2)
    {
        if (is_prime[i] && is_prime[i + 2])
        {
            ls.push_back(i);
        }
    }

    for (int64_t t = 0; t < T; t++)
    {
        int64_t n;
        cin >> n;

        int64_t p = (-2 + sqrtl(4 + 4 * n)) / 2;

        auto itr = ranges::upper_bound(ls, p);

        if (itr == ls.begin())
        {
            cout << -1 << endl;
            continue;
        }

        itr--;

        cout << (*itr * (*itr + 2)) << endl;
    }

    return 0;
}
0