結果

問題 No.774 tatyamと素数大富豪
ユーザー PachicobuePachicobue
提出日時 2018-12-25 23:03:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,188 ms / 2,000 ms
コード長 1,380 bytes
コンパイル時間 1,204 ms
コンパイル使用メモリ 110,668 KB
実行使用メモリ 6,952 KB
最終ジャッジ日時 2024-04-09 02:47:39
合計ジャッジ時間 5,573 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,188 ms
6,820 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,948 KB
testcase_03 AC 7 ms
6,944 KB
testcase_04 AC 3 ms
6,948 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,948 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,952 KB
testcase_09 AC 32 ms
6,948 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 8 ms
6,948 KB
testcase_12 AC 553 ms
6,948 KB
testcase_13 AC 522 ms
6,948 KB
testcase_14 AC 8 ms
6,944 KB
testcase_15 AC 13 ms
6,948 KB
testcase_16 AC 8 ms
6,944 KB
testcase_17 AC 4 ms
6,948 KB
testcase_18 AC 8 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
using ll = long long;
using LL = __int128_t;
std::mt19937 mt{1};
ll power(const LL a, const LL n, const ll mod) { return n == 0 ? 1 : n % 2 == 1 ? power(a, n - 1, mod) * a % mod : power(a * a % mod, n / 2, mod); }
bool MillerRabin(const ll n, const int rep = 2)
{
    if (n == 1) return false;
    ll s = 0, d = n - 1;
    for (; d % 2 == 0; s++, d /= 2) {}
    std::uniform_int_distribution<ll> dist{1, n - 1};
    for (int i = 0; i < rep; i++) {
        const ll a = dist(mt);
        if (power(a, n - 1, n) != 1) { return false; }
        ll num = power(a, d, n);
        if (num == 1) { continue; }
        bool comp = true;
        for (ll r = 0; r < s; r++) {
            if (num == n - 1) {
                comp = false;
                break;
            }
            num = (LL)num * (LL)num % n;
        }
        if (comp) { return false; }
    }
    return true;
}

int main()
{
    int N;
    std::cin >> N;
    std::vector<std::string> A(N);
    for (int i = 0; i < N; i++) { std::cin >> A[i]; }
    ll ans = -1;
    do {
        std::string s;
        for (const auto& e : A) { s += e; }
        const ll p = std::stoll(s);
        if (MillerRabin(p)) { ans = std::max(ans, p); }
    } while (std::next_permutation(A.begin(), A.end()));
    std::cout << ans << std::endl;
    return 0;
}
0